xref: /openbmc/linux/arch/s390/boot/startup.c (revision a44e4f3a)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/string.h>
3 #include <linux/elf.h>
4 #include <asm/sections.h>
5 #include <asm/setup.h>
6 #include <asm/kexec.h>
7 #include <asm/sclp.h>
8 #include <asm/diag.h>
9 #include <asm/uv.h>
10 #include "compressed/decompressor.h"
11 #include "boot.h"
12 
13 extern char __boot_data_start[], __boot_data_end[];
14 extern char __boot_data_preserved_start[], __boot_data_preserved_end[];
15 unsigned long __bootdata_preserved(__kaslr_offset);
16 
17 /*
18  * Some code and data needs to stay below 2 GB, even when the kernel would be
19  * relocated above 2 GB, because it has to use 31 bit addresses.
20  * Such code and data is part of the .dma section, and its location is passed
21  * over to the decompressed / relocated kernel via the .boot.preserved.data
22  * section.
23  */
24 extern char _sdma[], _edma[];
25 extern char _stext_dma[], _etext_dma[];
26 extern struct exception_table_entry _start_dma_ex_table[];
27 extern struct exception_table_entry _stop_dma_ex_table[];
28 unsigned long __bootdata_preserved(__sdma) = __pa(&_sdma);
29 unsigned long __bootdata_preserved(__edma) = __pa(&_edma);
30 unsigned long __bootdata_preserved(__stext_dma) = __pa(&_stext_dma);
31 unsigned long __bootdata_preserved(__etext_dma) = __pa(&_etext_dma);
32 struct exception_table_entry *
33 	__bootdata_preserved(__start_dma_ex_table) = _start_dma_ex_table;
34 struct exception_table_entry *
35 	__bootdata_preserved(__stop_dma_ex_table) = _stop_dma_ex_table;
36 
37 int _diag210_dma(struct diag210 *addr);
38 int _diag26c_dma(void *req, void *resp, enum diag26c_sc subcode);
39 int _diag14_dma(unsigned long rx, unsigned long ry1, unsigned long subcode);
40 void _diag0c_dma(struct hypfs_diag0c_entry *entry);
41 void _diag308_reset_dma(void);
42 struct diag_ops __bootdata_preserved(diag_dma_ops) = {
43 	.diag210 = _diag210_dma,
44 	.diag26c = _diag26c_dma,
45 	.diag14 = _diag14_dma,
46 	.diag0c = _diag0c_dma,
47 	.diag308_reset = _diag308_reset_dma
48 };
49 static struct diag210 _diag210_tmp_dma __section(".dma.data");
50 struct diag210 *__bootdata_preserved(__diag210_tmp_dma) = &_diag210_tmp_dma;
51 void _swsusp_reset_dma(void);
52 unsigned long __bootdata_preserved(__swsusp_reset_dma) = __pa(_swsusp_reset_dma);
53 
54 void error(char *x)
55 {
56 	sclp_early_printk("\n\n");
57 	sclp_early_printk(x);
58 	sclp_early_printk("\n\n -- System halted");
59 
60 	disabled_wait();
61 }
62 
63 #ifdef CONFIG_KERNEL_UNCOMPRESSED
64 unsigned long mem_safe_offset(void)
65 {
66 	return vmlinux.default_lma + vmlinux.image_size + vmlinux.bss_size;
67 }
68 #endif
69 
70 static void rescue_initrd(unsigned long addr)
71 {
72 	if (!IS_ENABLED(CONFIG_BLK_DEV_INITRD))
73 		return;
74 	if (!INITRD_START || !INITRD_SIZE)
75 		return;
76 	if (addr <= INITRD_START)
77 		return;
78 	memmove((void *)addr, (void *)INITRD_START, INITRD_SIZE);
79 	INITRD_START = addr;
80 }
81 
82 static void copy_bootdata(void)
83 {
84 	if (__boot_data_end - __boot_data_start != vmlinux.bootdata_size)
85 		error(".boot.data section size mismatch");
86 	memcpy((void *)vmlinux.bootdata_off, __boot_data_start, vmlinux.bootdata_size);
87 	if (__boot_data_preserved_end - __boot_data_preserved_start != vmlinux.bootdata_preserved_size)
88 		error(".boot.preserved.data section size mismatch");
89 	memcpy((void *)vmlinux.bootdata_preserved_off, __boot_data_preserved_start, vmlinux.bootdata_preserved_size);
90 }
91 
92 static void handle_relocs(unsigned long offset)
93 {
94 	Elf64_Rela *rela_start, *rela_end, *rela;
95 	int r_type, r_sym, rc;
96 	Elf64_Addr loc, val;
97 	Elf64_Sym *dynsym;
98 
99 	rela_start = (Elf64_Rela *) vmlinux.rela_dyn_start;
100 	rela_end = (Elf64_Rela *) vmlinux.rela_dyn_end;
101 	dynsym = (Elf64_Sym *) vmlinux.dynsym_start;
102 	for (rela = rela_start; rela < rela_end; rela++) {
103 		loc = rela->r_offset + offset;
104 		val = rela->r_addend;
105 		r_sym = ELF64_R_SYM(rela->r_info);
106 		if (r_sym) {
107 			if (dynsym[r_sym].st_shndx != SHN_UNDEF)
108 				val += dynsym[r_sym].st_value + offset;
109 		} else {
110 			/*
111 			 * 0 == undefined symbol table index (STN_UNDEF),
112 			 * used for R_390_RELATIVE, only add KASLR offset
113 			 */
114 			val += offset;
115 		}
116 		r_type = ELF64_R_TYPE(rela->r_info);
117 		rc = arch_kexec_do_relocs(r_type, (void *) loc, val, 0);
118 		if (rc)
119 			error("Unknown relocation type");
120 	}
121 }
122 
123 static void clear_bss_section(void)
124 {
125 	memset((void *)vmlinux.default_lma + vmlinux.image_size, 0, vmlinux.bss_size);
126 }
127 
128 void startup_kernel(void)
129 {
130 	unsigned long random_lma;
131 	unsigned long safe_addr;
132 	void *img;
133 
134 	store_ipl_parmblock();
135 	safe_addr = mem_safe_offset();
136 	safe_addr = read_ipl_report(safe_addr);
137 	uv_query_info();
138 	rescue_initrd(safe_addr);
139 	sclp_early_read_info();
140 	setup_boot_command_line();
141 	parse_boot_command_line();
142 	setup_memory_end();
143 	detect_memory();
144 
145 	random_lma = __kaslr_offset = 0;
146 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && kaslr_enabled) {
147 		random_lma = get_random_base(safe_addr);
148 		if (random_lma) {
149 			__kaslr_offset = random_lma - vmlinux.default_lma;
150 			img = (void *)vmlinux.default_lma;
151 			vmlinux.default_lma += __kaslr_offset;
152 			vmlinux.entry += __kaslr_offset;
153 			vmlinux.bootdata_off += __kaslr_offset;
154 			vmlinux.bootdata_preserved_off += __kaslr_offset;
155 			vmlinux.rela_dyn_start += __kaslr_offset;
156 			vmlinux.rela_dyn_end += __kaslr_offset;
157 			vmlinux.dynsym_start += __kaslr_offset;
158 		}
159 	}
160 
161 	if (!IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED)) {
162 		img = decompress_kernel();
163 		memmove((void *)vmlinux.default_lma, img, vmlinux.image_size);
164 	} else if (__kaslr_offset)
165 		memcpy((void *)vmlinux.default_lma, img, vmlinux.image_size);
166 
167 	clear_bss_section();
168 	copy_bootdata();
169 	if (IS_ENABLED(CONFIG_RELOCATABLE))
170 		handle_relocs(__kaslr_offset);
171 
172 	if (__kaslr_offset) {
173 		/* Clear non-relocated kernel */
174 		if (IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED))
175 			memset(img, 0, vmlinux.image_size);
176 	}
177 	vmlinux.entry();
178 }
179