xref: /openbmc/linux/arch/s390/boot/startup.c (revision 90178c19)
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>
515426ca4SVasily Gorbik #include <asm/setup.h>
6805bc0bcSGerald Schaefer #include <asm/kexec.h>
77516fc11SVasily Gorbik #include <asm/sclp.h>
8a80313ffSGerald Schaefer #include <asm/diag.h>
95abb9351SVasily Gorbik #include <asm/uv.h>
108f75582aSVasily Gorbik #include "compressed/decompressor.h"
118f75582aSVasily Gorbik #include "boot.h"
128f75582aSVasily Gorbik 
13d1b52a43SVasily Gorbik extern char __boot_data_start[], __boot_data_end[];
14bf9921a9SGerald Schaefer extern char __boot_data_preserved_start[], __boot_data_preserved_end[];
15b2d24b97SGerald Schaefer unsigned long __bootdata_preserved(__kaslr_offset);
16d1b52a43SVasily Gorbik 
17a80313ffSGerald Schaefer /*
18a80313ffSGerald Schaefer  * Some code and data needs to stay below 2 GB, even when the kernel would be
19a80313ffSGerald Schaefer  * relocated above 2 GB, because it has to use 31 bit addresses.
20a80313ffSGerald Schaefer  * Such code and data is part of the .dma section, and its location is passed
21a80313ffSGerald Schaefer  * over to the decompressed / relocated kernel via the .boot.preserved.data
22a80313ffSGerald Schaefer  * section.
23a80313ffSGerald Schaefer  */
24a80313ffSGerald Schaefer extern char _sdma[], _edma[];
25a80313ffSGerald Schaefer extern char _stext_dma[], _etext_dma[];
26a80313ffSGerald Schaefer extern struct exception_table_entry _start_dma_ex_table[];
27a80313ffSGerald Schaefer extern struct exception_table_entry _stop_dma_ex_table[];
28a80313ffSGerald Schaefer unsigned long __bootdata_preserved(__sdma) = __pa(&_sdma);
29a80313ffSGerald Schaefer unsigned long __bootdata_preserved(__edma) = __pa(&_edma);
30a80313ffSGerald Schaefer unsigned long __bootdata_preserved(__stext_dma) = __pa(&_stext_dma);
31a80313ffSGerald Schaefer unsigned long __bootdata_preserved(__etext_dma) = __pa(&_etext_dma);
32a80313ffSGerald Schaefer struct exception_table_entry *
33a80313ffSGerald Schaefer 	__bootdata_preserved(__start_dma_ex_table) = _start_dma_ex_table;
34a80313ffSGerald Schaefer struct exception_table_entry *
35a80313ffSGerald Schaefer 	__bootdata_preserved(__stop_dma_ex_table) = _stop_dma_ex_table;
36a80313ffSGerald Schaefer 
37a80313ffSGerald Schaefer int _diag210_dma(struct diag210 *addr);
38a80313ffSGerald Schaefer int _diag26c_dma(void *req, void *resp, enum diag26c_sc subcode);
39a80313ffSGerald Schaefer int _diag14_dma(unsigned long rx, unsigned long ry1, unsigned long subcode);
40a80313ffSGerald Schaefer void _diag0c_dma(struct hypfs_diag0c_entry *entry);
41a80313ffSGerald Schaefer void _diag308_reset_dma(void);
42a80313ffSGerald Schaefer struct diag_ops __bootdata_preserved(diag_dma_ops) = {
43a80313ffSGerald Schaefer 	.diag210 = _diag210_dma,
44a80313ffSGerald Schaefer 	.diag26c = _diag26c_dma,
45a80313ffSGerald Schaefer 	.diag14 = _diag14_dma,
46a80313ffSGerald Schaefer 	.diag0c = _diag0c_dma,
47a80313ffSGerald Schaefer 	.diag308_reset = _diag308_reset_dma
48a80313ffSGerald Schaefer };
4933def849SJoe Perches static struct diag210 _diag210_tmp_dma __section(".dma.data");
50a80313ffSGerald Schaefer struct diag210 *__bootdata_preserved(__diag210_tmp_dma) = &_diag210_tmp_dma;
51a80313ffSGerald Schaefer 
527516fc11SVasily Gorbik void error(char *x)
537516fc11SVasily Gorbik {
547516fc11SVasily Gorbik 	sclp_early_printk("\n\n");
557516fc11SVasily Gorbik 	sclp_early_printk(x);
567516fc11SVasily Gorbik 	sclp_early_printk("\n\n -- System halted");
577516fc11SVasily Gorbik 
5898587c2dSMartin Schwidefsky 	disabled_wait();
597516fc11SVasily Gorbik }
607516fc11SVasily Gorbik 
6115426ca4SVasily Gorbik #ifdef CONFIG_KERNEL_UNCOMPRESSED
6215426ca4SVasily Gorbik unsigned long mem_safe_offset(void)
6315426ca4SVasily Gorbik {
6415426ca4SVasily Gorbik 	return vmlinux.default_lma + vmlinux.image_size + vmlinux.bss_size;
6515426ca4SVasily Gorbik }
6615426ca4SVasily Gorbik #endif
6715426ca4SVasily Gorbik 
689641b8ccSMartin Schwidefsky static void rescue_initrd(unsigned long addr)
6915426ca4SVasily Gorbik {
7015426ca4SVasily Gorbik 	if (!IS_ENABLED(CONFIG_BLK_DEV_INITRD))
7115426ca4SVasily Gorbik 		return;
7215426ca4SVasily Gorbik 	if (!INITRD_START || !INITRD_SIZE)
7315426ca4SVasily Gorbik 		return;
749641b8ccSMartin Schwidefsky 	if (addr <= INITRD_START)
7515426ca4SVasily Gorbik 		return;
769641b8ccSMartin Schwidefsky 	memmove((void *)addr, (void *)INITRD_START, INITRD_SIZE);
779641b8ccSMartin Schwidefsky 	INITRD_START = addr;
7815426ca4SVasily Gorbik }
7915426ca4SVasily Gorbik 
80d1b52a43SVasily Gorbik static void copy_bootdata(void)
81d1b52a43SVasily Gorbik {
82d1b52a43SVasily Gorbik 	if (__boot_data_end - __boot_data_start != vmlinux.bootdata_size)
83d1b52a43SVasily Gorbik 		error(".boot.data section size mismatch");
84d1b52a43SVasily Gorbik 	memcpy((void *)vmlinux.bootdata_off, __boot_data_start, vmlinux.bootdata_size);
85bf9921a9SGerald Schaefer 	if (__boot_data_preserved_end - __boot_data_preserved_start != vmlinux.bootdata_preserved_size)
86bf9921a9SGerald Schaefer 		error(".boot.preserved.data section size mismatch");
87bf9921a9SGerald Schaefer 	memcpy((void *)vmlinux.bootdata_preserved_off, __boot_data_preserved_start, vmlinux.bootdata_preserved_size);
88d1b52a43SVasily Gorbik }
89d1b52a43SVasily Gorbik 
90805bc0bcSGerald Schaefer static void handle_relocs(unsigned long offset)
91805bc0bcSGerald Schaefer {
92805bc0bcSGerald Schaefer 	Elf64_Rela *rela_start, *rela_end, *rela;
93805bc0bcSGerald Schaefer 	int r_type, r_sym, rc;
94805bc0bcSGerald Schaefer 	Elf64_Addr loc, val;
95805bc0bcSGerald Schaefer 	Elf64_Sym *dynsym;
96805bc0bcSGerald Schaefer 
97805bc0bcSGerald Schaefer 	rela_start = (Elf64_Rela *) vmlinux.rela_dyn_start;
98805bc0bcSGerald Schaefer 	rela_end = (Elf64_Rela *) vmlinux.rela_dyn_end;
99805bc0bcSGerald Schaefer 	dynsym = (Elf64_Sym *) vmlinux.dynsym_start;
100805bc0bcSGerald Schaefer 	for (rela = rela_start; rela < rela_end; rela++) {
101805bc0bcSGerald Schaefer 		loc = rela->r_offset + offset;
102ac49303dSGerald Schaefer 		val = rela->r_addend;
103805bc0bcSGerald Schaefer 		r_sym = ELF64_R_SYM(rela->r_info);
104ac49303dSGerald Schaefer 		if (r_sym) {
105ac49303dSGerald Schaefer 			if (dynsym[r_sym].st_shndx != SHN_UNDEF)
106ac49303dSGerald Schaefer 				val += dynsym[r_sym].st_value + offset;
107ac49303dSGerald Schaefer 		} else {
108ac49303dSGerald Schaefer 			/*
109ac49303dSGerald Schaefer 			 * 0 == undefined symbol table index (STN_UNDEF),
110ac49303dSGerald Schaefer 			 * used for R_390_RELATIVE, only add KASLR offset
111ac49303dSGerald Schaefer 			 */
112ac49303dSGerald Schaefer 			val += offset;
113ac49303dSGerald Schaefer 		}
114805bc0bcSGerald Schaefer 		r_type = ELF64_R_TYPE(rela->r_info);
115805bc0bcSGerald Schaefer 		rc = arch_kexec_do_relocs(r_type, (void *) loc, val, 0);
116805bc0bcSGerald Schaefer 		if (rc)
117805bc0bcSGerald Schaefer 			error("Unknown relocation type");
118805bc0bcSGerald Schaefer 	}
119805bc0bcSGerald Schaefer }
120805bc0bcSGerald Schaefer 
121980d5f9aSAlexander Egorenkov /*
122980d5f9aSAlexander Egorenkov  * This function clears the BSS section of the decompressed Linux kernel and NOT the decompressor's.
123980d5f9aSAlexander Egorenkov  */
1242e83e0ebSVasily Gorbik static void clear_bss_section(void)
1252e83e0ebSVasily Gorbik {
1262e83e0ebSVasily Gorbik 	memset((void *)vmlinux.default_lma + vmlinux.image_size, 0, vmlinux.bss_size);
1272e83e0ebSVasily Gorbik }
1282e83e0ebSVasily Gorbik 
129*90178c19SHeiko Carstens /*
130*90178c19SHeiko Carstens  * Set vmalloc area size to an 8th of (potential) physical memory
131*90178c19SHeiko Carstens  * size, unless size has been set by kernel command line parameter.
132*90178c19SHeiko Carstens  */
133*90178c19SHeiko Carstens static void setup_vmalloc_size(void)
134*90178c19SHeiko Carstens {
135*90178c19SHeiko Carstens 	unsigned long size;
136*90178c19SHeiko Carstens 
137*90178c19SHeiko Carstens 	if (vmalloc_size_set)
138*90178c19SHeiko Carstens 		return;
139*90178c19SHeiko Carstens 	size = (memory_end ?: max_physmem_end) >> 3;
140*90178c19SHeiko Carstens 	size = round_up(size, _SEGMENT_SIZE);
141*90178c19SHeiko Carstens 	vmalloc_size = max(size, vmalloc_size);
142*90178c19SHeiko Carstens }
143*90178c19SHeiko Carstens 
1448f75582aSVasily Gorbik void startup_kernel(void)
1458f75582aSVasily Gorbik {
146b2d24b97SGerald Schaefer 	unsigned long random_lma;
1479641b8ccSMartin Schwidefsky 	unsigned long safe_addr;
148369f91c3SVasily Gorbik 	void *img;
1498f75582aSVasily Gorbik 
15049698745SVasily Gorbik 	store_ipl_parmblock();
1519641b8ccSMartin Schwidefsky 	safe_addr = mem_safe_offset();
1529641b8ccSMartin Schwidefsky 	safe_addr = read_ipl_report(safe_addr);
1539641b8ccSMartin Schwidefsky 	uv_query_info();
1549641b8ccSMartin Schwidefsky 	rescue_initrd(safe_addr);
1559641b8ccSMartin Schwidefsky 	sclp_early_read_info();
15649698745SVasily Gorbik 	setup_boot_command_line();
157b5e80459SVasily Gorbik 	parse_boot_command_line();
15849698745SVasily Gorbik 	setup_memory_end();
1596966d604SVasily Gorbik 	detect_memory();
160*90178c19SHeiko Carstens 	setup_vmalloc_size();
161b2d24b97SGerald Schaefer 
162b2d24b97SGerald Schaefer 	random_lma = __kaslr_offset = 0;
163b2d24b97SGerald Schaefer 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && kaslr_enabled) {
164b2d24b97SGerald Schaefer 		random_lma = get_random_base(safe_addr);
165b2d24b97SGerald Schaefer 		if (random_lma) {
166b2d24b97SGerald Schaefer 			__kaslr_offset = random_lma - vmlinux.default_lma;
167b2d24b97SGerald Schaefer 			img = (void *)vmlinux.default_lma;
168b2d24b97SGerald Schaefer 			vmlinux.default_lma += __kaslr_offset;
169b2d24b97SGerald Schaefer 			vmlinux.entry += __kaslr_offset;
170b2d24b97SGerald Schaefer 			vmlinux.bootdata_off += __kaslr_offset;
171b2d24b97SGerald Schaefer 			vmlinux.bootdata_preserved_off += __kaslr_offset;
172b2d24b97SGerald Schaefer 			vmlinux.rela_dyn_start += __kaslr_offset;
173b2d24b97SGerald Schaefer 			vmlinux.rela_dyn_end += __kaslr_offset;
174b2d24b97SGerald Schaefer 			vmlinux.dynsym_start += __kaslr_offset;
175b2d24b97SGerald Schaefer 		}
176b2d24b97SGerald Schaefer 	}
177b2d24b97SGerald Schaefer 
1788f75582aSVasily Gorbik 	if (!IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED)) {
179369f91c3SVasily Gorbik 		img = decompress_kernel();
180369f91c3SVasily Gorbik 		memmove((void *)vmlinux.default_lma, img, vmlinux.image_size);
181b2d24b97SGerald Schaefer 	} else if (__kaslr_offset)
182b2d24b97SGerald Schaefer 		memcpy((void *)vmlinux.default_lma, img, vmlinux.image_size);
183b2d24b97SGerald Schaefer 
1842e83e0ebSVasily Gorbik 	clear_bss_section();
185d1b52a43SVasily Gorbik 	copy_bootdata();
186805bc0bcSGerald Schaefer 	if (IS_ENABLED(CONFIG_RELOCATABLE))
187b2d24b97SGerald Schaefer 		handle_relocs(__kaslr_offset);
188b2d24b97SGerald Schaefer 
189b2d24b97SGerald Schaefer 	if (__kaslr_offset) {
190a9f2f686SGerald Schaefer 		/*
191a9f2f686SGerald Schaefer 		 * Save KASLR offset for early dumps, before vmcore_info is set.
192a9f2f686SGerald Schaefer 		 * Mark as uneven to distinguish from real vmcore_info pointer.
193a9f2f686SGerald Schaefer 		 */
194a9f2f686SGerald Schaefer 		S390_lowcore.vmcore_info = __kaslr_offset | 0x1UL;
195b2d24b97SGerald Schaefer 		/* Clear non-relocated kernel */
196b2d24b97SGerald Schaefer 		if (IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED))
197b2d24b97SGerald Schaefer 			memset(img, 0, vmlinux.image_size);
198b2d24b97SGerald Schaefer 	}
199369f91c3SVasily Gorbik 	vmlinux.entry();
2008f75582aSVasily Gorbik }
201