s390/early: rewrite program parameter setup in C
[sfrench/cifs-2.6.git] / arch / s390 / boot / startup.c
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/cpu_mf.h>
6 #include <asm/setup.h>
7 #include <asm/kexec.h>
8 #include <asm/sclp.h>
9 #include <asm/diag.h>
10 #include <asm/uv.h>
11 #include "compressed/decompressor.h"
12 #include "boot.h"
13
14 extern char __boot_data_start[], __boot_data_end[];
15 extern char __boot_data_preserved_start[], __boot_data_preserved_end[];
16 unsigned long __bootdata_preserved(__kaslr_offset);
17
18 /*
19  * Some code and data needs to stay below 2 GB, even when the kernel would be
20  * relocated above 2 GB, because it has to use 31 bit addresses.
21  * Such code and data is part of the .dma section, and its location is passed
22  * over to the decompressed / relocated kernel via the .boot.preserved.data
23  * section.
24  */
25 extern char _sdma[], _edma[];
26 extern char _stext_dma[], _etext_dma[];
27 extern struct exception_table_entry _start_dma_ex_table[];
28 extern struct exception_table_entry _stop_dma_ex_table[];
29 unsigned long __bootdata_preserved(__sdma) = __pa(&_sdma);
30 unsigned long __bootdata_preserved(__edma) = __pa(&_edma);
31 unsigned long __bootdata_preserved(__stext_dma) = __pa(&_stext_dma);
32 unsigned long __bootdata_preserved(__etext_dma) = __pa(&_etext_dma);
33 struct exception_table_entry *
34         __bootdata_preserved(__start_dma_ex_table) = _start_dma_ex_table;
35 struct exception_table_entry *
36         __bootdata_preserved(__stop_dma_ex_table) = _stop_dma_ex_table;
37
38 int _diag210_dma(struct diag210 *addr);
39 int _diag26c_dma(void *req, void *resp, enum diag26c_sc subcode);
40 int _diag14_dma(unsigned long rx, unsigned long ry1, unsigned long subcode);
41 void _diag0c_dma(struct hypfs_diag0c_entry *entry);
42 void _diag308_reset_dma(void);
43 struct diag_ops __bootdata_preserved(diag_dma_ops) = {
44         .diag210 = _diag210_dma,
45         .diag26c = _diag26c_dma,
46         .diag14 = _diag14_dma,
47         .diag0c = _diag0c_dma,
48         .diag308_reset = _diag308_reset_dma
49 };
50 static struct diag210 _diag210_tmp_dma __section(".dma.data");
51 struct diag210 *__bootdata_preserved(__diag210_tmp_dma) = &_diag210_tmp_dma;
52
53 void error(char *x)
54 {
55         sclp_early_printk("\n\n");
56         sclp_early_printk(x);
57         sclp_early_printk("\n\n -- System halted");
58
59         disabled_wait();
60 }
61
62 static void setup_lpp(void)
63 {
64         S390_lowcore.current_pid = 0;
65         S390_lowcore.lpp = LPP_MAGIC;
66         if (test_facility(40))
67                 lpp(&S390_lowcore.lpp);
68 }
69
70 #ifdef CONFIG_KERNEL_UNCOMPRESSED
71 unsigned long mem_safe_offset(void)
72 {
73         return vmlinux.default_lma + vmlinux.image_size + vmlinux.bss_size;
74 }
75 #endif
76
77 static void rescue_initrd(unsigned long addr)
78 {
79         if (!IS_ENABLED(CONFIG_BLK_DEV_INITRD))
80                 return;
81         if (!INITRD_START || !INITRD_SIZE)
82                 return;
83         if (addr <= INITRD_START)
84                 return;
85         memmove((void *)addr, (void *)INITRD_START, INITRD_SIZE);
86         INITRD_START = addr;
87 }
88
89 static void copy_bootdata(void)
90 {
91         if (__boot_data_end - __boot_data_start != vmlinux.bootdata_size)
92                 error(".boot.data section size mismatch");
93         memcpy((void *)vmlinux.bootdata_off, __boot_data_start, vmlinux.bootdata_size);
94         if (__boot_data_preserved_end - __boot_data_preserved_start != vmlinux.bootdata_preserved_size)
95                 error(".boot.preserved.data section size mismatch");
96         memcpy((void *)vmlinux.bootdata_preserved_off, __boot_data_preserved_start, vmlinux.bootdata_preserved_size);
97 }
98
99 static void handle_relocs(unsigned long offset)
100 {
101         Elf64_Rela *rela_start, *rela_end, *rela;
102         int r_type, r_sym, rc;
103         Elf64_Addr loc, val;
104         Elf64_Sym *dynsym;
105
106         rela_start = (Elf64_Rela *) vmlinux.rela_dyn_start;
107         rela_end = (Elf64_Rela *) vmlinux.rela_dyn_end;
108         dynsym = (Elf64_Sym *) vmlinux.dynsym_start;
109         for (rela = rela_start; rela < rela_end; rela++) {
110                 loc = rela->r_offset + offset;
111                 val = rela->r_addend;
112                 r_sym = ELF64_R_SYM(rela->r_info);
113                 if (r_sym) {
114                         if (dynsym[r_sym].st_shndx != SHN_UNDEF)
115                                 val += dynsym[r_sym].st_value + offset;
116                 } else {
117                         /*
118                          * 0 == undefined symbol table index (STN_UNDEF),
119                          * used for R_390_RELATIVE, only add KASLR offset
120                          */
121                         val += offset;
122                 }
123                 r_type = ELF64_R_TYPE(rela->r_info);
124                 rc = arch_kexec_do_relocs(r_type, (void *) loc, val, 0);
125                 if (rc)
126                         error("Unknown relocation type");
127         }
128 }
129
130 /*
131  * This function clears the BSS section of the decompressed Linux kernel and NOT the decompressor's.
132  */
133 static void clear_bss_section(void)
134 {
135         memset((void *)vmlinux.default_lma + vmlinux.image_size, 0, vmlinux.bss_size);
136 }
137
138 /*
139  * Set vmalloc area size to an 8th of (potential) physical memory
140  * size, unless size has been set by kernel command line parameter.
141  */
142 static void setup_vmalloc_size(void)
143 {
144         unsigned long size;
145
146         if (vmalloc_size_set)
147                 return;
148         size = (memory_end ?: max_physmem_end) >> 3;
149         size = round_up(size, _SEGMENT_SIZE);
150         vmalloc_size = max(size, vmalloc_size);
151 }
152
153 void startup_kernel(void)
154 {
155         unsigned long random_lma;
156         unsigned long safe_addr;
157         void *img;
158
159         setup_lpp();
160         store_ipl_parmblock();
161         safe_addr = mem_safe_offset();
162         safe_addr = read_ipl_report(safe_addr);
163         uv_query_info();
164         rescue_initrd(safe_addr);
165         sclp_early_read_info();
166         setup_boot_command_line();
167         parse_boot_command_line();
168         setup_memory_end();
169         detect_memory();
170         setup_vmalloc_size();
171
172         random_lma = __kaslr_offset = 0;
173         if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && kaslr_enabled) {
174                 random_lma = get_random_base(safe_addr);
175                 if (random_lma) {
176                         __kaslr_offset = random_lma - vmlinux.default_lma;
177                         img = (void *)vmlinux.default_lma;
178                         vmlinux.default_lma += __kaslr_offset;
179                         vmlinux.entry += __kaslr_offset;
180                         vmlinux.bootdata_off += __kaslr_offset;
181                         vmlinux.bootdata_preserved_off += __kaslr_offset;
182                         vmlinux.rela_dyn_start += __kaslr_offset;
183                         vmlinux.rela_dyn_end += __kaslr_offset;
184                         vmlinux.dynsym_start += __kaslr_offset;
185                 }
186         }
187
188         if (!IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED)) {
189                 img = decompress_kernel();
190                 memmove((void *)vmlinux.default_lma, img, vmlinux.image_size);
191         } else if (__kaslr_offset)
192                 memcpy((void *)vmlinux.default_lma, img, vmlinux.image_size);
193
194         clear_bss_section();
195         copy_bootdata();
196         if (IS_ENABLED(CONFIG_RELOCATABLE))
197                 handle_relocs(__kaslr_offset);
198
199         if (__kaslr_offset) {
200                 /*
201                  * Save KASLR offset for early dumps, before vmcore_info is set.
202                  * Mark as uneven to distinguish from real vmcore_info pointer.
203                  */
204                 S390_lowcore.vmcore_info = __kaslr_offset | 0x1UL;
205                 /* Clear non-relocated kernel */
206                 if (IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED))
207                         memset(img, 0, vmlinux.image_size);
208         }
209         vmlinux.entry();
210 }