xref: /openbmc/linux/arch/s390/boot/startup.c (revision d7e7fbba)
18f75582aSVasily Gorbik // SPDX-License-Identifier: GPL-2.0
28f75582aSVasily Gorbik #include <linux/string.h>
3805bc0bcSGerald Schaefer #include <linux/elf.h>
4a80313ffSGerald Schaefer #include <asm/sections.h>
5*d7e7fbbaSVasily Gorbik #include <asm/cpu_mf.h>
615426ca4SVasily Gorbik #include <asm/setup.h>
7805bc0bcSGerald Schaefer #include <asm/kexec.h>
87516fc11SVasily Gorbik #include <asm/sclp.h>
9a80313ffSGerald Schaefer #include <asm/diag.h>
105abb9351SVasily Gorbik #include <asm/uv.h>
118f75582aSVasily Gorbik #include "compressed/decompressor.h"
128f75582aSVasily Gorbik #include "boot.h"
138f75582aSVasily Gorbik 
14d1b52a43SVasily Gorbik extern char __boot_data_start[], __boot_data_end[];
15bf9921a9SGerald Schaefer extern char __boot_data_preserved_start[], __boot_data_preserved_end[];
16b2d24b97SGerald Schaefer unsigned long __bootdata_preserved(__kaslr_offset);
17d1b52a43SVasily Gorbik 
18a80313ffSGerald Schaefer /*
19a80313ffSGerald Schaefer  * Some code and data needs to stay below 2 GB, even when the kernel would be
20a80313ffSGerald Schaefer  * relocated above 2 GB, because it has to use 31 bit addresses.
21a80313ffSGerald Schaefer  * Such code and data is part of the .dma section, and its location is passed
22a80313ffSGerald Schaefer  * over to the decompressed / relocated kernel via the .boot.preserved.data
23a80313ffSGerald Schaefer  * section.
24a80313ffSGerald Schaefer  */
25a80313ffSGerald Schaefer extern char _sdma[], _edma[];
26a80313ffSGerald Schaefer extern char _stext_dma[], _etext_dma[];
27a80313ffSGerald Schaefer extern struct exception_table_entry _start_dma_ex_table[];
28a80313ffSGerald Schaefer extern struct exception_table_entry _stop_dma_ex_table[];
29a80313ffSGerald Schaefer unsigned long __bootdata_preserved(__sdma) = __pa(&_sdma);
30a80313ffSGerald Schaefer unsigned long __bootdata_preserved(__edma) = __pa(&_edma);
31a80313ffSGerald Schaefer unsigned long __bootdata_preserved(__stext_dma) = __pa(&_stext_dma);
32a80313ffSGerald Schaefer unsigned long __bootdata_preserved(__etext_dma) = __pa(&_etext_dma);
33a80313ffSGerald Schaefer struct exception_table_entry *
34a80313ffSGerald Schaefer 	__bootdata_preserved(__start_dma_ex_table) = _start_dma_ex_table;
35a80313ffSGerald Schaefer struct exception_table_entry *
36a80313ffSGerald Schaefer 	__bootdata_preserved(__stop_dma_ex_table) = _stop_dma_ex_table;
37a80313ffSGerald Schaefer 
38a80313ffSGerald Schaefer int _diag210_dma(struct diag210 *addr);
39a80313ffSGerald Schaefer int _diag26c_dma(void *req, void *resp, enum diag26c_sc subcode);
40a80313ffSGerald Schaefer int _diag14_dma(unsigned long rx, unsigned long ry1, unsigned long subcode);
41a80313ffSGerald Schaefer void _diag0c_dma(struct hypfs_diag0c_entry *entry);
42a80313ffSGerald Schaefer void _diag308_reset_dma(void);
43a80313ffSGerald Schaefer struct diag_ops __bootdata_preserved(diag_dma_ops) = {
44a80313ffSGerald Schaefer 	.diag210 = _diag210_dma,
45a80313ffSGerald Schaefer 	.diag26c = _diag26c_dma,
46a80313ffSGerald Schaefer 	.diag14 = _diag14_dma,
47a80313ffSGerald Schaefer 	.diag0c = _diag0c_dma,
48a80313ffSGerald Schaefer 	.diag308_reset = _diag308_reset_dma
49a80313ffSGerald Schaefer };
5033def849SJoe Perches static struct diag210 _diag210_tmp_dma __section(".dma.data");
51a80313ffSGerald Schaefer struct diag210 *__bootdata_preserved(__diag210_tmp_dma) = &_diag210_tmp_dma;
52a80313ffSGerald Schaefer 
537516fc11SVasily Gorbik void error(char *x)
547516fc11SVasily Gorbik {
557516fc11SVasily Gorbik 	sclp_early_printk("\n\n");
567516fc11SVasily Gorbik 	sclp_early_printk(x);
577516fc11SVasily Gorbik 	sclp_early_printk("\n\n -- System halted");
587516fc11SVasily Gorbik 
5998587c2dSMartin Schwidefsky 	disabled_wait();
607516fc11SVasily Gorbik }
617516fc11SVasily Gorbik 
62*d7e7fbbaSVasily Gorbik static void setup_lpp(void)
63*d7e7fbbaSVasily Gorbik {
64*d7e7fbbaSVasily Gorbik 	S390_lowcore.current_pid = 0;
65*d7e7fbbaSVasily Gorbik 	S390_lowcore.lpp = LPP_MAGIC;
66*d7e7fbbaSVasily Gorbik 	if (test_facility(40))
67*d7e7fbbaSVasily Gorbik 		lpp(&S390_lowcore.lpp);
68*d7e7fbbaSVasily Gorbik }
69*d7e7fbbaSVasily 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 
779641b8ccSMartin Schwidefsky static void rescue_initrd(unsigned long addr)
7815426ca4SVasily Gorbik {
7915426ca4SVasily Gorbik 	if (!IS_ENABLED(CONFIG_BLK_DEV_INITRD))
8015426ca4SVasily Gorbik 		return;
8115426ca4SVasily Gorbik 	if (!INITRD_START || !INITRD_SIZE)
8215426ca4SVasily Gorbik 		return;
839641b8ccSMartin Schwidefsky 	if (addr <= INITRD_START)
8415426ca4SVasily Gorbik 		return;
859641b8ccSMartin Schwidefsky 	memmove((void *)addr, (void *)INITRD_START, INITRD_SIZE);
869641b8ccSMartin Schwidefsky 	INITRD_START = addr;
8715426ca4SVasily Gorbik }
8815426ca4SVasily Gorbik 
89d1b52a43SVasily Gorbik static void copy_bootdata(void)
90d1b52a43SVasily Gorbik {
91d1b52a43SVasily Gorbik 	if (__boot_data_end - __boot_data_start != vmlinux.bootdata_size)
92d1b52a43SVasily Gorbik 		error(".boot.data section size mismatch");
93d1b52a43SVasily Gorbik 	memcpy((void *)vmlinux.bootdata_off, __boot_data_start, vmlinux.bootdata_size);
94bf9921a9SGerald Schaefer 	if (__boot_data_preserved_end - __boot_data_preserved_start != vmlinux.bootdata_preserved_size)
95bf9921a9SGerald Schaefer 		error(".boot.preserved.data section size mismatch");
96bf9921a9SGerald Schaefer 	memcpy((void *)vmlinux.bootdata_preserved_off, __boot_data_preserved_start, vmlinux.bootdata_preserved_size);
97d1b52a43SVasily Gorbik }
98d1b52a43SVasily Gorbik 
99805bc0bcSGerald Schaefer static void handle_relocs(unsigned long offset)
100805bc0bcSGerald Schaefer {
101805bc0bcSGerald Schaefer 	Elf64_Rela *rela_start, *rela_end, *rela;
102805bc0bcSGerald Schaefer 	int r_type, r_sym, rc;
103805bc0bcSGerald Schaefer 	Elf64_Addr loc, val;
104805bc0bcSGerald Schaefer 	Elf64_Sym *dynsym;
105805bc0bcSGerald Schaefer 
106805bc0bcSGerald Schaefer 	rela_start = (Elf64_Rela *) vmlinux.rela_dyn_start;
107805bc0bcSGerald Schaefer 	rela_end = (Elf64_Rela *) vmlinux.rela_dyn_end;
108805bc0bcSGerald Schaefer 	dynsym = (Elf64_Sym *) vmlinux.dynsym_start;
109805bc0bcSGerald Schaefer 	for (rela = rela_start; rela < rela_end; rela++) {
110805bc0bcSGerald Schaefer 		loc = rela->r_offset + offset;
111ac49303dSGerald Schaefer 		val = rela->r_addend;
112805bc0bcSGerald Schaefer 		r_sym = ELF64_R_SYM(rela->r_info);
113ac49303dSGerald Schaefer 		if (r_sym) {
114ac49303dSGerald Schaefer 			if (dynsym[r_sym].st_shndx != SHN_UNDEF)
115ac49303dSGerald Schaefer 				val += dynsym[r_sym].st_value + offset;
116ac49303dSGerald Schaefer 		} else {
117ac49303dSGerald Schaefer 			/*
118ac49303dSGerald Schaefer 			 * 0 == undefined symbol table index (STN_UNDEF),
119ac49303dSGerald Schaefer 			 * used for R_390_RELATIVE, only add KASLR offset
120ac49303dSGerald Schaefer 			 */
121ac49303dSGerald Schaefer 			val += offset;
122ac49303dSGerald Schaefer 		}
123805bc0bcSGerald Schaefer 		r_type = ELF64_R_TYPE(rela->r_info);
124805bc0bcSGerald Schaefer 		rc = arch_kexec_do_relocs(r_type, (void *) loc, val, 0);
125805bc0bcSGerald Schaefer 		if (rc)
126805bc0bcSGerald Schaefer 			error("Unknown relocation type");
127805bc0bcSGerald Schaefer 	}
128805bc0bcSGerald Schaefer }
129805bc0bcSGerald Schaefer 
130980d5f9aSAlexander Egorenkov /*
131980d5f9aSAlexander Egorenkov  * This function clears the BSS section of the decompressed Linux kernel and NOT the decompressor's.
132980d5f9aSAlexander Egorenkov  */
1332e83e0ebSVasily Gorbik static void clear_bss_section(void)
1342e83e0ebSVasily Gorbik {
1352e83e0ebSVasily Gorbik 	memset((void *)vmlinux.default_lma + vmlinux.image_size, 0, vmlinux.bss_size);
1362e83e0ebSVasily Gorbik }
1372e83e0ebSVasily Gorbik 
13890178c19SHeiko Carstens /*
13990178c19SHeiko Carstens  * Set vmalloc area size to an 8th of (potential) physical memory
14090178c19SHeiko Carstens  * size, unless size has been set by kernel command line parameter.
14190178c19SHeiko Carstens  */
14290178c19SHeiko Carstens static void setup_vmalloc_size(void)
14390178c19SHeiko Carstens {
14490178c19SHeiko Carstens 	unsigned long size;
14590178c19SHeiko Carstens 
14690178c19SHeiko Carstens 	if (vmalloc_size_set)
14790178c19SHeiko Carstens 		return;
14890178c19SHeiko Carstens 	size = (memory_end ?: max_physmem_end) >> 3;
14990178c19SHeiko Carstens 	size = round_up(size, _SEGMENT_SIZE);
15090178c19SHeiko Carstens 	vmalloc_size = max(size, vmalloc_size);
15190178c19SHeiko Carstens }
15290178c19SHeiko Carstens 
1538f75582aSVasily Gorbik void startup_kernel(void)
1548f75582aSVasily Gorbik {
155b2d24b97SGerald Schaefer 	unsigned long random_lma;
1569641b8ccSMartin Schwidefsky 	unsigned long safe_addr;
157369f91c3SVasily Gorbik 	void *img;
1588f75582aSVasily Gorbik 
159*d7e7fbbaSVasily Gorbik 	setup_lpp();
16049698745SVasily Gorbik 	store_ipl_parmblock();
1619641b8ccSMartin Schwidefsky 	safe_addr = mem_safe_offset();
1629641b8ccSMartin Schwidefsky 	safe_addr = read_ipl_report(safe_addr);
1639641b8ccSMartin Schwidefsky 	uv_query_info();
1649641b8ccSMartin Schwidefsky 	rescue_initrd(safe_addr);
1659641b8ccSMartin Schwidefsky 	sclp_early_read_info();
16649698745SVasily Gorbik 	setup_boot_command_line();
167b5e80459SVasily Gorbik 	parse_boot_command_line();
16849698745SVasily Gorbik 	setup_memory_end();
1696966d604SVasily Gorbik 	detect_memory();
17090178c19SHeiko Carstens 	setup_vmalloc_size();
171b2d24b97SGerald Schaefer 
172b2d24b97SGerald Schaefer 	random_lma = __kaslr_offset = 0;
173b2d24b97SGerald Schaefer 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && kaslr_enabled) {
174b2d24b97SGerald Schaefer 		random_lma = get_random_base(safe_addr);
175b2d24b97SGerald Schaefer 		if (random_lma) {
176b2d24b97SGerald Schaefer 			__kaslr_offset = random_lma - vmlinux.default_lma;
177b2d24b97SGerald Schaefer 			img = (void *)vmlinux.default_lma;
178b2d24b97SGerald Schaefer 			vmlinux.default_lma += __kaslr_offset;
179b2d24b97SGerald Schaefer 			vmlinux.entry += __kaslr_offset;
180b2d24b97SGerald Schaefer 			vmlinux.bootdata_off += __kaslr_offset;
181b2d24b97SGerald Schaefer 			vmlinux.bootdata_preserved_off += __kaslr_offset;
182b2d24b97SGerald Schaefer 			vmlinux.rela_dyn_start += __kaslr_offset;
183b2d24b97SGerald Schaefer 			vmlinux.rela_dyn_end += __kaslr_offset;
184b2d24b97SGerald Schaefer 			vmlinux.dynsym_start += __kaslr_offset;
185b2d24b97SGerald Schaefer 		}
186b2d24b97SGerald Schaefer 	}
187b2d24b97SGerald Schaefer 
1888f75582aSVasily Gorbik 	if (!IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED)) {
189369f91c3SVasily Gorbik 		img = decompress_kernel();
190369f91c3SVasily Gorbik 		memmove((void *)vmlinux.default_lma, img, vmlinux.image_size);
191b2d24b97SGerald Schaefer 	} else if (__kaslr_offset)
192b2d24b97SGerald Schaefer 		memcpy((void *)vmlinux.default_lma, img, vmlinux.image_size);
193b2d24b97SGerald Schaefer 
1942e83e0ebSVasily Gorbik 	clear_bss_section();
195d1b52a43SVasily Gorbik 	copy_bootdata();
196805bc0bcSGerald Schaefer 	if (IS_ENABLED(CONFIG_RELOCATABLE))
197b2d24b97SGerald Schaefer 		handle_relocs(__kaslr_offset);
198b2d24b97SGerald Schaefer 
199b2d24b97SGerald Schaefer 	if (__kaslr_offset) {
200a9f2f686SGerald Schaefer 		/*
201a9f2f686SGerald Schaefer 		 * Save KASLR offset for early dumps, before vmcore_info is set.
202a9f2f686SGerald Schaefer 		 * Mark as uneven to distinguish from real vmcore_info pointer.
203a9f2f686SGerald Schaefer 		 */
204a9f2f686SGerald Schaefer 		S390_lowcore.vmcore_info = __kaslr_offset | 0x1UL;
205b2d24b97SGerald Schaefer 		/* Clear non-relocated kernel */
206b2d24b97SGerald Schaefer 		if (IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED))
207b2d24b97SGerald Schaefer 			memset(img, 0, vmlinux.image_size);
208b2d24b97SGerald Schaefer 	}
209369f91c3SVasily Gorbik 	vmlinux.entry();
2108f75582aSVasily Gorbik }
211