xref: /openbmc/linux/arch/s390/boot/startup.c (revision 5cfdff02)
18f75582aSVasily Gorbik // SPDX-License-Identifier: GPL-2.0
28f75582aSVasily Gorbik #include <linux/string.h>
3805bc0bcSGerald Schaefer #include <linux/elf.h>
473045a08SVasily Gorbik #include <asm/boot_data.h>
5a80313ffSGerald Schaefer #include <asm/sections.h>
68e9205d2SAlexander Gordeev #include <asm/maccess.h>
7d7e7fbbaSVasily Gorbik #include <asm/cpu_mf.h>
815426ca4SVasily Gorbik #include <asm/setup.h>
90c4f2623SVasily Gorbik #include <asm/kasan.h>
10805bc0bcSGerald Schaefer #include <asm/kexec.h>
117516fc11SVasily Gorbik #include <asm/sclp.h>
12a80313ffSGerald Schaefer #include <asm/diag.h>
135abb9351SVasily Gorbik #include <asm/uv.h>
144df29d2bSAlexander Gordeev #include <asm/abs_lowcore.h>
158c37cb7dSVasily Gorbik #include <asm/physmem_info.h>
16edd4a866SHeiko Carstens #include "decompressor.h"
178f75582aSVasily Gorbik #include "boot.h"
18c5cf5054SAlexander Egorenkov #include "uv.h"
198f75582aSVasily Gorbik 
20b2d24b97SGerald Schaefer unsigned long __bootdata_preserved(__kaslr_offset);
214df29d2bSAlexander Gordeev unsigned long __bootdata_preserved(__abs_lowcore);
222f0e8aaeSAlexander Gordeev unsigned long __bootdata_preserved(__memcpy_real_area);
238e9205d2SAlexander Gordeev pte_t *__bootdata_preserved(memcpy_real_ptep);
240c4f2623SVasily Gorbik unsigned long __bootdata_preserved(VMALLOC_START);
250c4f2623SVasily Gorbik unsigned long __bootdata_preserved(VMALLOC_END);
260c4f2623SVasily Gorbik struct page *__bootdata_preserved(vmemmap);
270c4f2623SVasily Gorbik unsigned long __bootdata_preserved(vmemmap_size);
280c4f2623SVasily Gorbik unsigned long __bootdata_preserved(MODULES_VADDR);
290c4f2623SVasily Gorbik unsigned long __bootdata_preserved(MODULES_END);
3094fd5220SAlexander Gordeev unsigned long __bootdata_preserved(max_mappable);
3173045a08SVasily Gorbik unsigned long __bootdata(ident_map_size);
32d1b52a43SVasily Gorbik 
3317e89e13SSven Schnelle u64 __bootdata_preserved(stfle_fac_list[16]);
3417e89e13SSven Schnelle u64 __bootdata_preserved(alt_stfle_fac_list[16]);
35e9e7870fSAlexander Egorenkov struct oldmem_data __bootdata_preserved(oldmem_data);
3617e89e13SSven Schnelle 
37bd50b743SAlexander Gordeev struct machine_info machine;
38bd50b743SAlexander Gordeev 
397516fc11SVasily Gorbik void error(char *x)
407516fc11SVasily Gorbik {
417516fc11SVasily Gorbik 	sclp_early_printk("\n\n");
427516fc11SVasily Gorbik 	sclp_early_printk(x);
437516fc11SVasily Gorbik 	sclp_early_printk("\n\n -- System halted");
447516fc11SVasily Gorbik 
4598587c2dSMartin Schwidefsky 	disabled_wait();
467516fc11SVasily Gorbik }
477516fc11SVasily Gorbik 
48bd50b743SAlexander Gordeev static void detect_facilities(void)
49bd50b743SAlexander Gordeev {
50bd50b743SAlexander Gordeev 	if (test_facility(8)) {
51bd50b743SAlexander Gordeev 		machine.has_edat1 = 1;
52bd50b743SAlexander Gordeev 		__ctl_set_bit(0, 23);
53bd50b743SAlexander Gordeev 	}
54bd50b743SAlexander Gordeev 	if (test_facility(78))
55bd50b743SAlexander Gordeev 		machine.has_edat2 = 1;
56bd50b743SAlexander Gordeev 	if (!noexec_disabled && test_facility(130)) {
57bd50b743SAlexander Gordeev 		machine.has_nx = 1;
58bd50b743SAlexander Gordeev 		__ctl_set_bit(0, 20);
59bd50b743SAlexander Gordeev 	}
60bd50b743SAlexander Gordeev }
61bd50b743SAlexander Gordeev 
62d7e7fbbaSVasily Gorbik static void setup_lpp(void)
63d7e7fbbaSVasily Gorbik {
64d7e7fbbaSVasily Gorbik 	S390_lowcore.current_pid = 0;
65d7e7fbbaSVasily Gorbik 	S390_lowcore.lpp = LPP_MAGIC;
66d7e7fbbaSVasily Gorbik 	if (test_facility(40))
67d7e7fbbaSVasily Gorbik 		lpp(&S390_lowcore.lpp);
68d7e7fbbaSVasily Gorbik }
69d7e7fbbaSVasily Gorbik 
7015426ca4SVasily Gorbik #ifdef CONFIG_KERNEL_UNCOMPRESSED
7115426ca4SVasily Gorbik unsigned long mem_safe_offset(void)
7215426ca4SVasily Gorbik {
7315426ca4SVasily Gorbik 	return vmlinux.default_lma + vmlinux.image_size + vmlinux.bss_size;
7415426ca4SVasily Gorbik }
7515426ca4SVasily Gorbik #endif
7615426ca4SVasily Gorbik 
77f913a660SVasily Gorbik static void rescue_initrd(unsigned long min, unsigned long max)
7815426ca4SVasily Gorbik {
79f913a660SVasily Gorbik 	unsigned long old_addr, addr, size;
80f913a660SVasily Gorbik 
8115426ca4SVasily Gorbik 	if (!IS_ENABLED(CONFIG_BLK_DEV_INITRD))
82f913a660SVasily Gorbik 		return;
83f913a660SVasily Gorbik 	if (!get_physmem_reserved(RR_INITRD, &addr, &size))
84f913a660SVasily Gorbik 		return;
85f913a660SVasily Gorbik 	if (addr >= min && addr + size <= max)
86f913a660SVasily Gorbik 		return;
87f913a660SVasily Gorbik 	old_addr = addr;
88f913a660SVasily Gorbik 	physmem_free(RR_INITRD);
89f913a660SVasily Gorbik 	addr = physmem_alloc_top_down(RR_INITRD, size, 0);
90f913a660SVasily Gorbik 	memmove((void *)addr, (void *)old_addr, size);
9115426ca4SVasily Gorbik }
9215426ca4SVasily Gorbik 
93d1b52a43SVasily Gorbik static void copy_bootdata(void)
94d1b52a43SVasily Gorbik {
95d1b52a43SVasily Gorbik 	if (__boot_data_end - __boot_data_start != vmlinux.bootdata_size)
96d1b52a43SVasily Gorbik 		error(".boot.data section size mismatch");
97d1b52a43SVasily Gorbik 	memcpy((void *)vmlinux.bootdata_off, __boot_data_start, vmlinux.bootdata_size);
98bf9921a9SGerald Schaefer 	if (__boot_data_preserved_end - __boot_data_preserved_start != vmlinux.bootdata_preserved_size)
99bf9921a9SGerald Schaefer 		error(".boot.preserved.data section size mismatch");
100bf9921a9SGerald Schaefer 	memcpy((void *)vmlinux.bootdata_preserved_off, __boot_data_preserved_start, vmlinux.bootdata_preserved_size);
101d1b52a43SVasily Gorbik }
102d1b52a43SVasily Gorbik 
103805bc0bcSGerald Schaefer static void handle_relocs(unsigned long offset)
104805bc0bcSGerald Schaefer {
105805bc0bcSGerald Schaefer 	Elf64_Rela *rela_start, *rela_end, *rela;
106805bc0bcSGerald Schaefer 	int r_type, r_sym, rc;
107805bc0bcSGerald Schaefer 	Elf64_Addr loc, val;
108805bc0bcSGerald Schaefer 	Elf64_Sym *dynsym;
109805bc0bcSGerald Schaefer 
110805bc0bcSGerald Schaefer 	rela_start = (Elf64_Rela *) vmlinux.rela_dyn_start;
111805bc0bcSGerald Schaefer 	rela_end = (Elf64_Rela *) vmlinux.rela_dyn_end;
112805bc0bcSGerald Schaefer 	dynsym = (Elf64_Sym *) vmlinux.dynsym_start;
113805bc0bcSGerald Schaefer 	for (rela = rela_start; rela < rela_end; rela++) {
114805bc0bcSGerald Schaefer 		loc = rela->r_offset + offset;
115ac49303dSGerald Schaefer 		val = rela->r_addend;
116805bc0bcSGerald Schaefer 		r_sym = ELF64_R_SYM(rela->r_info);
117ac49303dSGerald Schaefer 		if (r_sym) {
118ac49303dSGerald Schaefer 			if (dynsym[r_sym].st_shndx != SHN_UNDEF)
119ac49303dSGerald Schaefer 				val += dynsym[r_sym].st_value + offset;
120ac49303dSGerald Schaefer 		} else {
121ac49303dSGerald Schaefer 			/*
122ac49303dSGerald Schaefer 			 * 0 == undefined symbol table index (STN_UNDEF),
123ac49303dSGerald Schaefer 			 * used for R_390_RELATIVE, only add KASLR offset
124ac49303dSGerald Schaefer 			 */
125ac49303dSGerald Schaefer 			val += offset;
126ac49303dSGerald Schaefer 		}
127805bc0bcSGerald Schaefer 		r_type = ELF64_R_TYPE(rela->r_info);
128805bc0bcSGerald Schaefer 		rc = arch_kexec_do_relocs(r_type, (void *) loc, val, 0);
129805bc0bcSGerald Schaefer 		if (rc)
130805bc0bcSGerald Schaefer 			error("Unknown relocation type");
131805bc0bcSGerald Schaefer 	}
132805bc0bcSGerald Schaefer }
133805bc0bcSGerald Schaefer 
134980d5f9aSAlexander Egorenkov /*
13573045a08SVasily Gorbik  * Merge information from several sources into a single ident_map_size value.
13673045a08SVasily Gorbik  * "ident_map_size" represents the upper limit of physical memory we may ever
13773045a08SVasily Gorbik  * reach. It might not be all online memory, but also include standby (offline)
13873045a08SVasily Gorbik  * memory. "ident_map_size" could be lower then actual standby or even online
13973045a08SVasily Gorbik  * memory present, due to limiting factors. We should never go above this limit.
14073045a08SVasily Gorbik  * It is the size of our identity mapping.
14173045a08SVasily Gorbik  *
14273045a08SVasily Gorbik  * Consider the following factors:
14373045a08SVasily Gorbik  * 1. max_physmem_end - end of physical memory online or standby.
1448c37cb7dSVasily Gorbik  *    Always >= end of the last online memory range (get_physmem_online_end()).
14573045a08SVasily Gorbik  * 2. CONFIG_MAX_PHYSMEM_BITS - the maximum size of physical memory the
14673045a08SVasily Gorbik  *    kernel is able to support.
14773045a08SVasily Gorbik  * 3. "mem=" kernel command line option which limits physical memory usage.
14873045a08SVasily Gorbik  * 4. OLDMEM_BASE which is a kdump memory limit when the kernel is executed as
14973045a08SVasily Gorbik  *    crash kernel.
15073045a08SVasily Gorbik  * 5. "hsa" size which is a memory limit when the kernel is executed during
15173045a08SVasily Gorbik  *    zfcp/nvme dump.
15273045a08SVasily Gorbik  */
15373045a08SVasily Gorbik static void setup_ident_map_size(unsigned long max_physmem_end)
15473045a08SVasily Gorbik {
15573045a08SVasily Gorbik 	unsigned long hsa_size;
15673045a08SVasily Gorbik 
15773045a08SVasily Gorbik 	ident_map_size = max_physmem_end;
15873045a08SVasily Gorbik 	if (memory_limit)
15973045a08SVasily Gorbik 		ident_map_size = min(ident_map_size, memory_limit);
16073045a08SVasily Gorbik 	ident_map_size = min(ident_map_size, 1UL << MAX_PHYSMEM_BITS);
16173045a08SVasily Gorbik 
16273045a08SVasily Gorbik #ifdef CONFIG_CRASH_DUMP
163e9e7870fSAlexander Egorenkov 	if (oldmem_data.start) {
164bb87190cSHeiko Carstens 		__kaslr_enabled = 0;
165e9e7870fSAlexander Egorenkov 		ident_map_size = min(ident_map_size, oldmem_data.size);
16673045a08SVasily Gorbik 	} else if (ipl_block_valid && is_ipl_block_dump()) {
167bb87190cSHeiko Carstens 		__kaslr_enabled = 0;
16873045a08SVasily Gorbik 		if (!sclp_early_get_hsa_size(&hsa_size) && hsa_size)
16973045a08SVasily Gorbik 			ident_map_size = min(ident_map_size, hsa_size);
17073045a08SVasily Gorbik 	}
17173045a08SVasily Gorbik #endif
17273045a08SVasily Gorbik }
17373045a08SVasily Gorbik 
174bb1520d5SAlexander Gordeev static unsigned long setup_kernel_memory_layout(void)
1750c4f2623SVasily Gorbik {
1769a39abb7SVasily Gorbik 	unsigned long vmemmap_start;
177bb1520d5SAlexander Gordeev 	unsigned long asce_limit;
1780c4f2623SVasily Gorbik 	unsigned long rte_size;
1790c4f2623SVasily Gorbik 	unsigned long pages;
1808ddccc8aSAlexander Gordeev 	unsigned long vsize;
18157ad19bcSAlexander Gordeev 	unsigned long vmax;
1820c4f2623SVasily Gorbik 
1830c4f2623SVasily Gorbik 	pages = ident_map_size / PAGE_SIZE;
1840c4f2623SVasily Gorbik 	/* vmemmap contains a multiple of PAGES_PER_SECTION struct pages */
1850c4f2623SVasily Gorbik 	vmemmap_size = SECTION_ALIGN_UP(pages) * sizeof(struct page);
1860c4f2623SVasily Gorbik 
1870c4f2623SVasily Gorbik 	/* choose kernel address space layout: 4 or 3 levels. */
18809cd4ffaSAlexander Gordeev 	vsize = round_up(ident_map_size, _REGION3_SIZE) + vmemmap_size +
18909cd4ffaSAlexander Gordeev 		MODULES_LEN + MEMCPY_REAL_SIZE + ABS_LOWCORE_MAP_SIZE;
1908ddccc8aSAlexander Gordeev 	vsize = size_add(vsize, vmalloc_size);
1918ddccc8aSAlexander Gordeev 	if (IS_ENABLED(CONFIG_KASAN) || (vsize > _REGION2_SIZE)) {
192bb1520d5SAlexander Gordeev 		asce_limit = _REGION1_SIZE;
1939a39abb7SVasily Gorbik 		rte_size = _REGION2_SIZE;
1949a39abb7SVasily Gorbik 	} else {
195bb1520d5SAlexander Gordeev 		asce_limit = _REGION2_SIZE;
1969a39abb7SVasily Gorbik 		rte_size = _REGION3_SIZE;
1979a39abb7SVasily Gorbik 	}
198*5cfdff02SAlexander Gordeev 
1990c4f2623SVasily Gorbik 	/*
200*5cfdff02SAlexander Gordeev 	 * Forcing modules and vmalloc area under the ultravisor
2010c4f2623SVasily Gorbik 	 * secure storage limit, so that any vmalloc allocation
2020c4f2623SVasily Gorbik 	 * we do could be used to back secure guest storage.
2030c4f2623SVasily Gorbik 	 */
204bb1520d5SAlexander Gordeev 	vmax = adjust_to_uv_max(asce_limit);
2050c4f2623SVasily Gorbik #ifdef CONFIG_KASAN
2060c4f2623SVasily Gorbik 	/* force vmalloc and modules below kasan shadow */
20757ad19bcSAlexander Gordeev 	vmax = min(vmax, KASAN_SHADOW_START);
2080c4f2623SVasily Gorbik #endif
209a984f27eSAlexander Gordeev 	__memcpy_real_area = round_down(vmax - MEMCPY_REAL_SIZE, PAGE_SIZE);
2102f0e8aaeSAlexander Gordeev 	__abs_lowcore = round_down(__memcpy_real_area - ABS_LOWCORE_MAP_SIZE,
2112f0e8aaeSAlexander Gordeev 				   sizeof(struct lowcore));
2124df29d2bSAlexander Gordeev 	MODULES_END = round_down(__abs_lowcore, _SEGMENT_SIZE);
2130c4f2623SVasily Gorbik 	MODULES_VADDR = MODULES_END - MODULES_LEN;
2140c4f2623SVasily Gorbik 	VMALLOC_END = MODULES_VADDR;
2150c4f2623SVasily Gorbik 
2169a39abb7SVasily Gorbik 	/* allow vmalloc area to occupy up to about 1/2 of the rest virtual space left */
2179a39abb7SVasily Gorbik 	vmalloc_size = min(vmalloc_size, round_down(VMALLOC_END / 2, _REGION3_SIZE));
2180c4f2623SVasily Gorbik 	VMALLOC_START = VMALLOC_END - vmalloc_size;
2190c4f2623SVasily Gorbik 
2209a39abb7SVasily Gorbik 	/* split remaining virtual space between 1:1 mapping & vmemmap array */
2219a39abb7SVasily Gorbik 	pages = VMALLOC_START / (PAGE_SIZE + sizeof(struct page));
2220c4f2623SVasily Gorbik 	pages = SECTION_ALIGN_UP(pages);
2239a39abb7SVasily Gorbik 	/* keep vmemmap_start aligned to a top level region table entry */
2249a39abb7SVasily Gorbik 	vmemmap_start = round_down(VMALLOC_START - pages * sizeof(struct page), rte_size);
2259a39abb7SVasily Gorbik 	vmemmap_start = min(vmemmap_start, 1UL << MAX_PHYSMEM_BITS);
22694fd5220SAlexander Gordeev 	/* maximum mappable address as seen by arch_get_mappable_range() */
22794fd5220SAlexander Gordeev 	max_mappable = vmemmap_start;
2289a39abb7SVasily Gorbik 	/* make sure identity map doesn't overlay with vmemmap */
2299a39abb7SVasily Gorbik 	ident_map_size = min(ident_map_size, vmemmap_start);
2300c4f2623SVasily Gorbik 	vmemmap_size = SECTION_ALIGN_UP(ident_map_size / PAGE_SIZE) * sizeof(struct page);
2319a39abb7SVasily Gorbik 	/* make sure vmemmap doesn't overlay with vmalloc area */
2329a39abb7SVasily Gorbik 	VMALLOC_START = max(vmemmap_start + vmemmap_size, VMALLOC_START);
2339a39abb7SVasily Gorbik 	vmemmap = (struct page *)vmemmap_start;
234bb1520d5SAlexander Gordeev 
235bb1520d5SAlexander Gordeev 	return asce_limit;
2360c4f2623SVasily Gorbik }
2370c4f2623SVasily Gorbik 
23873045a08SVasily Gorbik /*
239980d5f9aSAlexander Egorenkov  * This function clears the BSS section of the decompressed Linux kernel and NOT the decompressor's.
240980d5f9aSAlexander Egorenkov  */
241a1d2d9cbSVasily Gorbik static void clear_bss_section(unsigned long vmlinux_lma)
2422e83e0ebSVasily Gorbik {
243a1d2d9cbSVasily Gorbik 	memset((void *)vmlinux_lma + vmlinux.image_size, 0, vmlinux.bss_size);
2442e83e0ebSVasily Gorbik }
2452e83e0ebSVasily Gorbik 
24690178c19SHeiko Carstens /*
24790178c19SHeiko Carstens  * Set vmalloc area size to an 8th of (potential) physical memory
24890178c19SHeiko Carstens  * size, unless size has been set by kernel command line parameter.
24990178c19SHeiko Carstens  */
25090178c19SHeiko Carstens static void setup_vmalloc_size(void)
25190178c19SHeiko Carstens {
25290178c19SHeiko Carstens 	unsigned long size;
25390178c19SHeiko Carstens 
25490178c19SHeiko Carstens 	if (vmalloc_size_set)
25590178c19SHeiko Carstens 		return;
25673045a08SVasily Gorbik 	size = round_up(ident_map_size / 8, _SEGMENT_SIZE);
25790178c19SHeiko Carstens 	vmalloc_size = max(size, vmalloc_size);
25890178c19SHeiko Carstens }
25990178c19SHeiko Carstens 
260e8f06683SAlexander Gordeev static void offset_vmlinux_info(unsigned long offset)
261e8f06683SAlexander Gordeev {
262e8f06683SAlexander Gordeev 	*(unsigned long *)(&vmlinux.entry) += offset;
263e8f06683SAlexander Gordeev 	vmlinux.bootdata_off += offset;
264e8f06683SAlexander Gordeev 	vmlinux.bootdata_preserved_off += offset;
265e8f06683SAlexander Gordeev 	vmlinux.rela_dyn_start += offset;
266e8f06683SAlexander Gordeev 	vmlinux.rela_dyn_end += offset;
267e8f06683SAlexander Gordeev 	vmlinux.dynsym_start += offset;
268bb1520d5SAlexander Gordeev 	vmlinux.init_mm_off += offset;
269bb1520d5SAlexander Gordeev 	vmlinux.swapper_pg_dir_off += offset;
270bb1520d5SAlexander Gordeev 	vmlinux.invalid_pg_dir_off += offset;
271557b1970SVasily Gorbik #ifdef CONFIG_KASAN
272557b1970SVasily Gorbik 	vmlinux.kasan_early_shadow_page_off += offset;
273557b1970SVasily Gorbik 	vmlinux.kasan_early_shadow_pte_off += offset;
274557b1970SVasily Gorbik 	vmlinux.kasan_early_shadow_pmd_off += offset;
275557b1970SVasily Gorbik 	vmlinux.kasan_early_shadow_pud_off += offset;
276557b1970SVasily Gorbik 	vmlinux.kasan_early_shadow_p4d_off += offset;
277557b1970SVasily Gorbik #endif
278e8f06683SAlexander Gordeev }
279e8f06683SAlexander Gordeev 
2808f75582aSVasily Gorbik void startup_kernel(void)
2818f75582aSVasily Gorbik {
28222476f47SVasily Gorbik 	unsigned long max_physmem_end;
283a1d2d9cbSVasily Gorbik 	unsigned long vmlinux_lma = 0;
284b3e0423cSVasily Gorbik 	unsigned long amode31_lma = 0;
285bb1520d5SAlexander Gordeev 	unsigned long asce_limit;
286f913a660SVasily Gorbik 	unsigned long safe_addr;
287369f91c3SVasily Gorbik 	void *img;
288bb1520d5SAlexander Gordeev 	psw_t psw;
2898f75582aSVasily Gorbik 
290f913a660SVasily Gorbik 	setup_lpp();
291f913a660SVasily Gorbik 	safe_addr = mem_safe_offset();
292*5cfdff02SAlexander Gordeev 
293f913a660SVasily Gorbik 	/*
294*5cfdff02SAlexander Gordeev 	 * Reserve decompressor memory together with decompression heap, buffer and
295f913a660SVasily Gorbik 	 * memory which might be occupied by uncompressed kernel at default 1Mb
296f913a660SVasily Gorbik 	 * position (if KASLR is off or failed).
297f913a660SVasily Gorbik 	 */
298f913a660SVasily Gorbik 	physmem_reserve(RR_DECOMPRESSOR, 0, safe_addr);
299f913a660SVasily Gorbik 	if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && parmarea.initrd_size)
300f913a660SVasily Gorbik 		physmem_reserve(RR_INITRD, parmarea.initrd_start, parmarea.initrd_size);
301e9e7870fSAlexander Egorenkov 	oldmem_data.start = parmarea.oldmem_base;
302e9e7870fSAlexander Egorenkov 	oldmem_data.size = parmarea.oldmem_size;
30384733284SAlexander Egorenkov 
30449698745SVasily Gorbik 	store_ipl_parmblock();
305f913a660SVasily Gorbik 	read_ipl_report();
3069641b8ccSMartin Schwidefsky 	uv_query_info();
3079641b8ccSMartin Schwidefsky 	sclp_early_read_info();
30849698745SVasily Gorbik 	setup_boot_command_line();
309b5e80459SVasily Gorbik 	parse_boot_command_line();
310d1725ca6SVasily Gorbik 	detect_facilities();
31142c89439SAlexander Egorenkov 	sanitize_prot_virt_host();
312f913a660SVasily Gorbik 	max_physmem_end = detect_max_physmem_end();
31322476f47SVasily Gorbik 	setup_ident_map_size(max_physmem_end);
31490178c19SHeiko Carstens 	setup_vmalloc_size();
315bb1520d5SAlexander Gordeev 	asce_limit = setup_kernel_memory_layout();
316f913a660SVasily Gorbik 	/* got final ident_map_size, physmem allocations could be performed now */
3178c37cb7dSVasily Gorbik 	physmem_set_usable_limit(ident_map_size);
318f913a660SVasily Gorbik 	detect_physmem_online_ranges(max_physmem_end);
319f913a660SVasily Gorbik 	save_ipl_cert_comp_list();
320f913a660SVasily Gorbik 	rescue_initrd(safe_addr, ident_map_size);
321b2d24b97SGerald Schaefer 
322bb87190cSHeiko Carstens 	if (kaslr_enabled()) {
3236e259bc5SVasily Gorbik 		vmlinux_lma = randomize_within_range(vmlinux.image_size + vmlinux.bss_size,
3246e259bc5SVasily Gorbik 						     THREAD_SIZE, vmlinux.default_lma,
3256e259bc5SVasily Gorbik 						     ident_map_size);
326a1d2d9cbSVasily Gorbik 		if (vmlinux_lma) {
327a1d2d9cbSVasily Gorbik 			__kaslr_offset = vmlinux_lma - vmlinux.default_lma;
328e8f06683SAlexander Gordeev 			offset_vmlinux_info(__kaslr_offset);
329b2d24b97SGerald Schaefer 		}
330b2d24b97SGerald Schaefer 	}
331a1d2d9cbSVasily Gorbik 	vmlinux_lma = vmlinux_lma ?: vmlinux.default_lma;
332a1d2d9cbSVasily Gorbik 	physmem_reserve(RR_VMLINUX, vmlinux_lma, vmlinux.image_size + vmlinux.bss_size);
333b2d24b97SGerald Schaefer 
3348f75582aSVasily Gorbik 	if (!IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED)) {
335369f91c3SVasily Gorbik 		img = decompress_kernel();
336a1d2d9cbSVasily Gorbik 		memmove((void *)vmlinux_lma, img, vmlinux.image_size);
337f913a660SVasily Gorbik 	} else if (__kaslr_offset) {
338a1d2d9cbSVasily Gorbik 		img = (void *)vmlinux.default_lma;
339a1d2d9cbSVasily Gorbik 		memmove((void *)vmlinux_lma, img, vmlinux.image_size);
340f913a660SVasily Gorbik 		memset(img, 0, vmlinux.image_size);
341f913a660SVasily Gorbik 	}
342f913a660SVasily Gorbik 
343f913a660SVasily Gorbik 	/* vmlinux decompression is done, shrink reserved low memory */
344f913a660SVasily Gorbik 	physmem_reserve(RR_DECOMPRESSOR, 0, (unsigned long)_decompressor_end);
345b3e0423cSVasily Gorbik 	if (kaslr_enabled())
346b3e0423cSVasily Gorbik 		amode31_lma = randomize_within_range(vmlinux.amode31_size, PAGE_SIZE, 0, SZ_2G);
347b3e0423cSVasily Gorbik 	amode31_lma = amode31_lma ?: vmlinux.default_lma - vmlinux.amode31_size;
34889843520SVasily Gorbik 	physmem_reserve(RR_AMODE31, amode31_lma, vmlinux.amode31_size);
349b2d24b97SGerald Schaefer 
350bb1520d5SAlexander Gordeev 	/*
351bb1520d5SAlexander Gordeev 	 * The order of the following operations is important:
352bb1520d5SAlexander Gordeev 	 *
353bb1520d5SAlexander Gordeev 	 * - handle_relocs() must follow clear_bss_section() to establish static
354bb1520d5SAlexander Gordeev 	 *   memory references to data in .bss to be used by setup_vmem()
355bb1520d5SAlexander Gordeev 	 *   (i.e init_mm.pgd)
356bb1520d5SAlexander Gordeev 	 *
357bb1520d5SAlexander Gordeev 	 * - setup_vmem() must follow handle_relocs() to be able using
358bb1520d5SAlexander Gordeev 	 *   static memory references to data in .bss (i.e init_mm.pgd)
359bb1520d5SAlexander Gordeev 	 *
360bb1520d5SAlexander Gordeev 	 * - copy_bootdata() must follow setup_vmem() to propagate changes to
361bb1520d5SAlexander Gordeev 	 *   bootdata made by setup_vmem()
362bb1520d5SAlexander Gordeev 	 */
363a1d2d9cbSVasily Gorbik 	clear_bss_section(vmlinux_lma);
364b2d24b97SGerald Schaefer 	handle_relocs(__kaslr_offset);
365bf64f051SVasily Gorbik 	setup_vmem(asce_limit);
366bb1520d5SAlexander Gordeev 	copy_bootdata();
367b2d24b97SGerald Schaefer 
368a9f2f686SGerald Schaefer 	/*
369a9f2f686SGerald Schaefer 	 * Save KASLR offset for early dumps, before vmcore_info is set.
370a9f2f686SGerald Schaefer 	 * Mark as uneven to distinguish from real vmcore_info pointer.
371a9f2f686SGerald Schaefer 	 */
372f913a660SVasily Gorbik 	S390_lowcore.vmcore_info = __kaslr_offset ? __kaslr_offset | 0x1UL : 0;
373bb1520d5SAlexander Gordeev 
374bb1520d5SAlexander Gordeev 	/*
375bb1520d5SAlexander Gordeev 	 * Jump to the decompressed kernel entry point and switch DAT mode on.
376bb1520d5SAlexander Gordeev 	 */
377bb1520d5SAlexander Gordeev 	psw.addr = vmlinux.entry;
378bb1520d5SAlexander Gordeev 	psw.mask = PSW_KERNEL_BITS;
379bb1520d5SAlexander Gordeev 	__load_psw(psw);
3808f75582aSVasily Gorbik }
381