1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/string.h>
4 #include <asm/boot_data.h>
5 #include <asm/sections.h>
6 #include <asm/cpu_mf.h>
13 #include <asm/abs_lowcore.h>
14 #include "decompressor.h"
18 unsigned long __bootdata_preserved(__kaslr_offset);
19 unsigned long __bootdata_preserved(__abs_lowcore);
20 unsigned long __bootdata(__amode31_base);
21 unsigned long __bootdata_preserved(VMALLOC_START);
22 unsigned long __bootdata_preserved(VMALLOC_END);
23 struct page *__bootdata_preserved(vmemmap);
24 unsigned long __bootdata_preserved(vmemmap_size);
25 unsigned long __bootdata_preserved(MODULES_VADDR);
26 unsigned long __bootdata_preserved(MODULES_END);
27 unsigned long __bootdata(ident_map_size);
28 int __bootdata(is_full_image) = 1;
29 struct initrd_data __bootdata(initrd_data);
31 u64 __bootdata_preserved(stfle_fac_list[16]);
32 u64 __bootdata_preserved(alt_stfle_fac_list[16]);
33 struct oldmem_data __bootdata_preserved(oldmem_data);
37 sclp_early_printk("\n\n");
39 sclp_early_printk("\n\n -- System halted");
44 static void setup_lpp(void)
46 S390_lowcore.current_pid = 0;
47 S390_lowcore.lpp = LPP_MAGIC;
48 if (test_facility(40))
49 lpp(&S390_lowcore.lpp);
52 #ifdef CONFIG_KERNEL_UNCOMPRESSED
53 unsigned long mem_safe_offset(void)
55 return vmlinux.default_lma + vmlinux.image_size + vmlinux.bss_size;
59 static void rescue_initrd(unsigned long addr)
61 if (!IS_ENABLED(CONFIG_BLK_DEV_INITRD))
63 if (!initrd_data.start || !initrd_data.size)
65 if (addr <= initrd_data.start)
67 memmove((void *)addr, (void *)initrd_data.start, initrd_data.size);
68 initrd_data.start = addr;
71 static void copy_bootdata(void)
73 if (__boot_data_end - __boot_data_start != vmlinux.bootdata_size)
74 error(".boot.data section size mismatch");
75 memcpy((void *)vmlinux.bootdata_off, __boot_data_start, vmlinux.bootdata_size);
76 if (__boot_data_preserved_end - __boot_data_preserved_start != vmlinux.bootdata_preserved_size)
77 error(".boot.preserved.data section size mismatch");
78 memcpy((void *)vmlinux.bootdata_preserved_off, __boot_data_preserved_start, vmlinux.bootdata_preserved_size);
81 static void handle_relocs(unsigned long offset)
83 Elf64_Rela *rela_start, *rela_end, *rela;
84 int r_type, r_sym, rc;
88 rela_start = (Elf64_Rela *) vmlinux.rela_dyn_start;
89 rela_end = (Elf64_Rela *) vmlinux.rela_dyn_end;
90 dynsym = (Elf64_Sym *) vmlinux.dynsym_start;
91 for (rela = rela_start; rela < rela_end; rela++) {
92 loc = rela->r_offset + offset;
94 r_sym = ELF64_R_SYM(rela->r_info);
96 if (dynsym[r_sym].st_shndx != SHN_UNDEF)
97 val += dynsym[r_sym].st_value + offset;
100 * 0 == undefined symbol table index (STN_UNDEF),
101 * used for R_390_RELATIVE, only add KASLR offset
105 r_type = ELF64_R_TYPE(rela->r_info);
106 rc = arch_kexec_do_relocs(r_type, (void *) loc, val, 0);
108 error("Unknown relocation type");
113 * Merge information from several sources into a single ident_map_size value.
114 * "ident_map_size" represents the upper limit of physical memory we may ever
115 * reach. It might not be all online memory, but also include standby (offline)
116 * memory. "ident_map_size" could be lower then actual standby or even online
117 * memory present, due to limiting factors. We should never go above this limit.
118 * It is the size of our identity mapping.
120 * Consider the following factors:
121 * 1. max_physmem_end - end of physical memory online or standby.
122 * Always <= end of the last online memory block (get_mem_detect_end()).
123 * 2. CONFIG_MAX_PHYSMEM_BITS - the maximum size of physical memory the
124 * kernel is able to support.
125 * 3. "mem=" kernel command line option which limits physical memory usage.
126 * 4. OLDMEM_BASE which is a kdump memory limit when the kernel is executed as
128 * 5. "hsa" size which is a memory limit when the kernel is executed during
131 static void setup_ident_map_size(unsigned long max_physmem_end)
133 unsigned long hsa_size;
135 ident_map_size = max_physmem_end;
137 ident_map_size = min(ident_map_size, memory_limit);
138 ident_map_size = min(ident_map_size, 1UL << MAX_PHYSMEM_BITS);
140 #ifdef CONFIG_CRASH_DUMP
141 if (oldmem_data.start) {
143 ident_map_size = min(ident_map_size, oldmem_data.size);
144 } else if (ipl_block_valid && is_ipl_block_dump()) {
146 if (!sclp_early_get_hsa_size(&hsa_size) && hsa_size)
147 ident_map_size = min(ident_map_size, hsa_size);
152 static void setup_kernel_memory_layout(void)
154 unsigned long vmemmap_start;
155 unsigned long rte_size;
159 pages = ident_map_size / PAGE_SIZE;
160 /* vmemmap contains a multiple of PAGES_PER_SECTION struct pages */
161 vmemmap_size = SECTION_ALIGN_UP(pages) * sizeof(struct page);
163 /* choose kernel address space layout: 4 or 3 levels. */
164 vmemmap_start = round_up(ident_map_size, _REGION3_SIZE);
165 if (IS_ENABLED(CONFIG_KASAN) ||
166 vmalloc_size > _REGION2_SIZE ||
167 vmemmap_start + vmemmap_size + vmalloc_size + MODULES_LEN >
169 vmax = _REGION1_SIZE;
170 rte_size = _REGION2_SIZE;
172 vmax = _REGION2_SIZE;
173 rte_size = _REGION3_SIZE;
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.
180 vmax = adjust_to_uv_max(vmax);
182 /* force vmalloc and modules below kasan shadow */
183 vmax = min(vmax, KASAN_SHADOW_START);
185 __abs_lowcore = round_down(vmax - ABS_LOWCORE_MAP_SIZE, sizeof(struct lowcore));
186 MODULES_END = round_down(__abs_lowcore, _SEGMENT_SIZE);
187 MODULES_VADDR = MODULES_END - MODULES_LEN;
188 VMALLOC_END = MODULES_VADDR;
190 /* allow vmalloc area to occupy up to about 1/2 of the rest virtual space left */
191 vmalloc_size = min(vmalloc_size, round_down(VMALLOC_END / 2, _REGION3_SIZE));
192 VMALLOC_START = VMALLOC_END - vmalloc_size;
194 /* split remaining virtual space between 1:1 mapping & vmemmap array */
195 pages = VMALLOC_START / (PAGE_SIZE + sizeof(struct page));
196 pages = SECTION_ALIGN_UP(pages);
197 /* keep vmemmap_start aligned to a top level region table entry */
198 vmemmap_start = round_down(VMALLOC_START - pages * sizeof(struct page), rte_size);
199 /* vmemmap_start is the future VMEM_MAX_PHYS, make sure it is within MAX_PHYSMEM */
200 vmemmap_start = min(vmemmap_start, 1UL << MAX_PHYSMEM_BITS);
201 /* make sure identity map doesn't overlay with vmemmap */
202 ident_map_size = min(ident_map_size, vmemmap_start);
203 vmemmap_size = SECTION_ALIGN_UP(ident_map_size / PAGE_SIZE) * sizeof(struct page);
204 /* make sure vmemmap doesn't overlay with vmalloc area */
205 VMALLOC_START = max(vmemmap_start + vmemmap_size, VMALLOC_START);
206 vmemmap = (struct page *)vmemmap_start;
210 * This function clears the BSS section of the decompressed Linux kernel and NOT the decompressor's.
212 static void clear_bss_section(void)
214 memset((void *)vmlinux.default_lma + vmlinux.image_size, 0, vmlinux.bss_size);
218 * Set vmalloc area size to an 8th of (potential) physical memory
219 * size, unless size has been set by kernel command line parameter.
221 static void setup_vmalloc_size(void)
225 if (vmalloc_size_set)
227 size = round_up(ident_map_size / 8, _SEGMENT_SIZE);
228 vmalloc_size = max(size, vmalloc_size);
231 static void offset_vmlinux_info(unsigned long offset)
233 vmlinux.default_lma += offset;
234 *(unsigned long *)(&vmlinux.entry) += offset;
235 vmlinux.bootdata_off += offset;
236 vmlinux.bootdata_preserved_off += offset;
237 vmlinux.rela_dyn_start += offset;
238 vmlinux.rela_dyn_end += offset;
239 vmlinux.dynsym_start += offset;
242 static unsigned long reserve_amode31(unsigned long safe_addr)
244 __amode31_base = PAGE_ALIGN(safe_addr);
245 return safe_addr + vmlinux.amode31_size;
248 void startup_kernel(void)
250 unsigned long random_lma;
251 unsigned long safe_addr;
254 initrd_data.start = parmarea.initrd_start;
255 initrd_data.size = parmarea.initrd_size;
256 oldmem_data.start = parmarea.oldmem_base;
257 oldmem_data.size = parmarea.oldmem_size;
260 store_ipl_parmblock();
261 safe_addr = mem_safe_offset();
262 safe_addr = reserve_amode31(safe_addr);
263 safe_addr = read_ipl_report(safe_addr);
265 rescue_initrd(safe_addr);
266 sclp_early_read_info();
267 setup_boot_command_line();
268 parse_boot_command_line();
269 sanitize_prot_virt_host();
270 setup_ident_map_size(detect_memory());
271 setup_vmalloc_size();
272 setup_kernel_memory_layout();
274 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && kaslr_enabled) {
275 random_lma = get_random_base(safe_addr);
277 __kaslr_offset = random_lma - vmlinux.default_lma;
278 img = (void *)vmlinux.default_lma;
279 offset_vmlinux_info(__kaslr_offset);
283 if (!IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED)) {
284 img = decompress_kernel();
285 memmove((void *)vmlinux.default_lma, img, vmlinux.image_size);
286 } else if (__kaslr_offset)
287 memcpy((void *)vmlinux.default_lma, img, vmlinux.image_size);
291 if (IS_ENABLED(CONFIG_RELOCATABLE))
292 handle_relocs(__kaslr_offset);
294 if (__kaslr_offset) {
296 * Save KASLR offset for early dumps, before vmcore_info is set.
297 * Mark as uneven to distinguish from real vmcore_info pointer.
299 S390_lowcore.vmcore_info = __kaslr_offset | 0x1UL;
300 /* Clear non-relocated kernel */
301 if (IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED))
302 memset(img, 0, vmlinux.image_size);