xref: /openbmc/linux/arch/s390/boot/startup.c (revision 86db9f28)
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 + offset;
105 		r_sym = ELF64_R_SYM(rela->r_info);
106 		if (r_sym)
107 			val += dynsym[r_sym].st_value;
108 		r_type = ELF64_R_TYPE(rela->r_info);
109 		rc = arch_kexec_do_relocs(r_type, (void *) loc, val, 0);
110 		if (rc)
111 			error("Unknown relocation type");
112 	}
113 }
114 
115 static void clear_bss_section(void)
116 {
117 	memset((void *)vmlinux.default_lma + vmlinux.image_size, 0, vmlinux.bss_size);
118 }
119 
120 void startup_kernel(void)
121 {
122 	unsigned long random_lma;
123 	unsigned long safe_addr;
124 	void *img;
125 
126 	store_ipl_parmblock();
127 	safe_addr = mem_safe_offset();
128 	safe_addr = read_ipl_report(safe_addr);
129 	uv_query_info();
130 	rescue_initrd(safe_addr);
131 	sclp_early_read_info();
132 	setup_boot_command_line();
133 	parse_boot_command_line();
134 	setup_memory_end();
135 	detect_memory();
136 
137 	random_lma = __kaslr_offset = 0;
138 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && kaslr_enabled) {
139 		random_lma = get_random_base(safe_addr);
140 		if (random_lma) {
141 			__kaslr_offset = random_lma - vmlinux.default_lma;
142 			img = (void *)vmlinux.default_lma;
143 			vmlinux.default_lma += __kaslr_offset;
144 			vmlinux.entry += __kaslr_offset;
145 			vmlinux.bootdata_off += __kaslr_offset;
146 			vmlinux.bootdata_preserved_off += __kaslr_offset;
147 			vmlinux.rela_dyn_start += __kaslr_offset;
148 			vmlinux.rela_dyn_end += __kaslr_offset;
149 			vmlinux.dynsym_start += __kaslr_offset;
150 		}
151 	}
152 
153 	if (!IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED)) {
154 		img = decompress_kernel();
155 		memmove((void *)vmlinux.default_lma, img, vmlinux.image_size);
156 	} else if (__kaslr_offset)
157 		memcpy((void *)vmlinux.default_lma, img, vmlinux.image_size);
158 
159 	clear_bss_section();
160 	copy_bootdata();
161 	if (IS_ENABLED(CONFIG_RELOCATABLE))
162 		handle_relocs(__kaslr_offset);
163 
164 	if (__kaslr_offset) {
165 		/* Clear non-relocated kernel */
166 		if (IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED))
167 			memset(img, 0, vmlinux.image_size);
168 	}
169 	vmlinux.entry();
170 }
171