1 // SPDX-License-Identifier: GPL-2.0
2 #define boot_fmt(fmt) "startup: " fmt
3 #include <linux/string.h>
4 #include <linux/elf.h>
5 #include <asm/page-states.h>
6 #include <asm/boot_data.h>
7 #include <asm/extmem.h>
8 #include <asm/sections.h>
9 #include <asm/maccess.h>
10 #include <asm/machine.h>
11 #include <asm/sysinfo.h>
12 #include <asm/cpu_mf.h>
13 #include <asm/setup.h>
14 #include <asm/timex.h>
15 #include <asm/kasan.h>
16 #include <asm/kexec.h>
17 #include <asm/sclp.h>
18 #include <asm/diag.h>
19 #include <asm/uv.h>
20 #include <asm/abs_lowcore.h>
21 #include <asm/physmem_info.h>
22 #include "decompressor.h"
23 #include "boot.h"
24 #include "uv.h"
25
26 struct vm_layout __bootdata_preserved(vm_layout);
27 unsigned long __bootdata_preserved(__abs_lowcore);
28 unsigned long __bootdata_preserved(__memcpy_real_area);
29 pte_t *__bootdata_preserved(memcpy_real_ptep);
30 unsigned long __bootdata_preserved(VMALLOC_START);
31 unsigned long __bootdata_preserved(VMALLOC_END);
32 struct page *__bootdata_preserved(vmemmap);
33 unsigned long __bootdata_preserved(vmemmap_size);
34 unsigned long __bootdata_preserved(MODULES_VADDR);
35 unsigned long __bootdata_preserved(MODULES_END);
36 unsigned long __bootdata_preserved(max_mappable);
37 unsigned long __bootdata_preserved(page_noexec_mask);
38 unsigned long __bootdata_preserved(segment_noexec_mask);
39 unsigned long __bootdata_preserved(region_noexec_mask);
40 union tod_clock __bootdata_preserved(tod_clock_base);
41 u64 __bootdata_preserved(clock_comparator_max) = -1UL;
42
43 u64 __bootdata_preserved(stfle_fac_list[16]);
44 struct oldmem_data __bootdata_preserved(oldmem_data);
45
error(char * x)46 void error(char *x)
47 {
48 boot_emerg("%s\n", x);
49 boot_emerg(" -- System halted\n");
50 disabled_wait();
51 }
52
53 static char sysinfo_page[PAGE_SIZE] __aligned(PAGE_SIZE);
54
detect_machine_type(void)55 static void detect_machine_type(void)
56 {
57 struct sysinfo_3_2_2 *vmms = (struct sysinfo_3_2_2 *)&sysinfo_page;
58
59 /* Check current-configuration-level */
60 if (stsi(NULL, 0, 0, 0) <= 2) {
61 set_machine_feature(MFEATURE_LPAR);
62 return;
63 }
64 /* Get virtual-machine cpu information. */
65 if (stsi(vmms, 3, 2, 2) || !vmms->count)
66 return;
67 /* Detect known hypervisors */
68 if (!memcmp(vmms->vm[0].cpi, "\xd2\xe5\xd4", 3))
69 set_machine_feature(MFEATURE_KVM);
70 else if (!memcmp(vmms->vm[0].cpi, "\xa9\x61\xe5\xd4", 4))
71 set_machine_feature(MFEATURE_VM);
72 }
73
detect_diag9c(void)74 static void detect_diag9c(void)
75 {
76 unsigned int cpu;
77 int rc = 1;
78
79 cpu = stap();
80 asm_inline volatile(
81 " diag %[cpu],%%r0,0x9c\n"
82 "0: lhi %[rc],0\n"
83 "1:\n"
84 EX_TABLE(0b, 1b)
85 : [rc] "+d" (rc)
86 : [cpu] "d" (cpu)
87 : "cc", "memory");
88 if (!rc)
89 set_machine_feature(MFEATURE_DIAG9C);
90 }
91
reset_tod_clock(void)92 static void reset_tod_clock(void)
93 {
94 union tod_clock clk;
95
96 if (store_tod_clock_ext_cc(&clk) == 0)
97 return;
98 /* TOD clock not running. Set the clock to Unix Epoch. */
99 if (set_tod_clock(TOD_UNIX_EPOCH) || store_tod_clock_ext_cc(&clk))
100 disabled_wait();
101 memset(&tod_clock_base, 0, sizeof(tod_clock_base));
102 tod_clock_base.tod = TOD_UNIX_EPOCH;
103 get_lowcore()->last_update_clock = TOD_UNIX_EPOCH;
104 }
105
detect_facilities(void)106 static void detect_facilities(void)
107 {
108 if (cpu_has_edat1())
109 local_ctl_set_bit(0, CR0_EDAT_BIT);
110 page_noexec_mask = -1UL;
111 segment_noexec_mask = -1UL;
112 region_noexec_mask = -1UL;
113 if (!cpu_has_nx()) {
114 page_noexec_mask &= ~_PAGE_NOEXEC;
115 segment_noexec_mask &= ~_SEGMENT_ENTRY_NOEXEC;
116 region_noexec_mask &= ~_REGION_ENTRY_NOEXEC;
117 }
118 if (IS_ENABLED(CONFIG_PCI) && test_facility(153))
119 set_machine_feature(MFEATURE_PCI_MIO);
120 reset_tod_clock();
121 if (test_facility(139) && (tod_clock_base.tod >> 63)) {
122 /* Enable signed clock comparator comparisons */
123 set_machine_feature(MFEATURE_SCC);
124 clock_comparator_max = -1UL >> 1;
125 local_ctl_set_bit(0, CR0_CLOCK_COMPARATOR_SIGN_BIT);
126 }
127 if (test_facility(50) && test_facility(73)) {
128 set_machine_feature(MFEATURE_TX);
129 local_ctl_set_bit(0, CR0_TRANSACTIONAL_EXECUTION_BIT);
130 }
131 if (cpu_has_vx())
132 local_ctl_set_bit(0, CR0_VECTOR_BIT);
133 }
134
cmma_test_essa(void)135 static int cmma_test_essa(void)
136 {
137 unsigned long tmp = 0;
138 int rc = 1;
139
140 /* Test ESSA_GET_STATE */
141 asm_inline volatile(
142 " .insn rrf,0xb9ab0000,%[tmp],%[tmp],%[cmd],0\n"
143 "0: lhi %[rc],0\n"
144 "1:\n"
145 EX_TABLE(0b, 1b)
146 : [rc] "+d" (rc), [tmp] "+d" (tmp)
147 : [cmd] "i" (ESSA_GET_STATE)
148 : "cc", "memory");
149 return rc;
150 }
151
cmma_init(void)152 static void cmma_init(void)
153 {
154 if (!cmma_flag)
155 return;
156 if (cmma_test_essa()) {
157 cmma_flag = 0;
158 return;
159 }
160 if (test_facility(147))
161 cmma_flag = 2;
162 }
163
setup_lpp(void)164 static void setup_lpp(void)
165 {
166 get_lowcore()->current_pid = 0;
167 get_lowcore()->lpp = LPP_MAGIC;
168 if (test_facility(40))
169 lpp(&get_lowcore()->lpp);
170 }
171
172 #ifdef CONFIG_KERNEL_UNCOMPRESSED
mem_safe_offset(void)173 static unsigned long mem_safe_offset(void)
174 {
175 return (unsigned long)_compressed_start;
176 }
177
deploy_kernel(void * output)178 static void deploy_kernel(void *output)
179 {
180 void *uncompressed_start = (void *)_compressed_start;
181
182 if (output == uncompressed_start)
183 return;
184 memmove(output, uncompressed_start, vmlinux.image_size);
185 memset(uncompressed_start, 0, vmlinux.image_size);
186 }
187 #endif
188
rescue_initrd(unsigned long min,unsigned long max)189 static void rescue_initrd(unsigned long min, unsigned long max)
190 {
191 unsigned long old_addr, addr, size;
192
193 if (!IS_ENABLED(CONFIG_BLK_DEV_INITRD))
194 return;
195 if (!get_physmem_reserved(RR_INITRD, &addr, &size))
196 return;
197 if (addr >= min && addr + size <= max)
198 return;
199 old_addr = addr;
200 physmem_free(RR_INITRD);
201 addr = physmem_alloc_or_die(RR_INITRD, size, 0);
202 memmove((void *)addr, (void *)old_addr, size);
203 }
204
copy_bootdata(void)205 static void copy_bootdata(void)
206 {
207 if (__boot_data_end - __boot_data_start != vmlinux.bootdata_size)
208 error(".boot.data section size mismatch");
209 memcpy((void *)vmlinux.bootdata_off, __boot_data_start, vmlinux.bootdata_size);
210 if (__boot_data_preserved_end - __boot_data_preserved_start != vmlinux.bootdata_preserved_size)
211 error(".boot.preserved.data section size mismatch");
212 memcpy((void *)vmlinux.bootdata_preserved_off, __boot_data_preserved_start, vmlinux.bootdata_preserved_size);
213 }
214
kaslr_adjust_relocs(unsigned long min_addr,unsigned long max_addr,unsigned long offset,unsigned long phys_offset)215 static void kaslr_adjust_relocs(unsigned long min_addr, unsigned long max_addr,
216 unsigned long offset, unsigned long phys_offset)
217 {
218 int *reloc;
219 long loc;
220
221 /* Adjust R_390_64 relocations */
222 for (reloc = (int *)__vmlinux_relocs_64_start; reloc < (int *)__vmlinux_relocs_64_end; reloc++) {
223 loc = (long)*reloc + phys_offset;
224 if (loc < min_addr || loc > max_addr)
225 error("64-bit relocation outside of kernel!\n");
226 *(u64 *)loc += offset;
227 }
228 }
229
kaslr_adjust_got(unsigned long offset)230 static void kaslr_adjust_got(unsigned long offset)
231 {
232 u64 *entry;
233
234 /*
235 * Adjust GOT entries, except for ones for undefined weak symbols
236 * that resolved to zero. This also skips the first three reserved
237 * entries on s390x that are zero.
238 */
239 for (entry = (u64 *)vmlinux.got_start; entry < (u64 *)vmlinux.got_end; entry++) {
240 if (*entry)
241 *entry += offset;
242 }
243 }
244
245 /*
246 * Merge information from several sources into a single ident_map_size value.
247 * "ident_map_size" represents the upper limit of physical memory we may ever
248 * reach. It might not be all online memory, but also include standby (offline)
249 * memory or memory areas reserved for other means (e.g., memory devices such as
250 * virtio-mem).
251 *
252 * "ident_map_size" could be lower then actual standby/reserved or even online
253 * memory present, due to limiting factors. We should never go above this limit.
254 * It is the size of our identity mapping.
255 *
256 * Consider the following factors:
257 * 1. max_physmem_end - end of physical memory online, standby or reserved.
258 * Always >= end of the last online memory range (get_physmem_online_end()).
259 * 2. CONFIG_MAX_PHYSMEM_BITS - the maximum size of physical memory the
260 * kernel is able to support.
261 * 3. "mem=" kernel command line option which limits physical memory usage.
262 * 4. OLDMEM_BASE which is a kdump memory limit when the kernel is executed as
263 * crash kernel.
264 * 5. "hsa" size which is a memory limit when the kernel is executed during
265 * zfcp/nvme dump.
266 */
setup_ident_map_size(unsigned long max_physmem_end)267 static void setup_ident_map_size(unsigned long max_physmem_end)
268 {
269 unsigned long hsa_size;
270
271 ident_map_size = max_physmem_end;
272 if (memory_limit)
273 ident_map_size = min(ident_map_size, memory_limit);
274 ident_map_size = min(ident_map_size, 1UL << MAX_PHYSMEM_BITS);
275
276 #ifdef CONFIG_CRASH_DUMP
277 if (oldmem_data.start) {
278 __kaslr_enabled = 0;
279 ident_map_size = min(ident_map_size, oldmem_data.size);
280 boot_debug("kdump memory limit: 0x%016lx\n", oldmem_data.size);
281 } else if (ipl_block_valid && is_ipl_block_dump()) {
282 __kaslr_enabled = 0;
283 if (!sclp_early_get_hsa_size(&hsa_size) && hsa_size) {
284 ident_map_size = min(ident_map_size, hsa_size);
285 boot_debug("Stand-alone dump limit: 0x%016lx\n", hsa_size);
286 }
287 }
288 #endif
289 boot_debug("Identity map size: 0x%016lx\n", ident_map_size);
290 }
291
292 #define FIXMAP_SIZE round_up(MEMCPY_REAL_SIZE + ABS_LOWCORE_MAP_SIZE, sizeof(struct lowcore))
293
get_vmem_size(unsigned long identity_size,unsigned long vmemmap_size,unsigned long vmalloc_size,unsigned long rte_size)294 static unsigned long get_vmem_size(unsigned long identity_size,
295 unsigned long vmemmap_size,
296 unsigned long vmalloc_size,
297 unsigned long rte_size)
298 {
299 unsigned long max_mappable, vsize;
300
301 max_mappable = max(identity_size, MAX_DCSS_ADDR);
302 vsize = round_up(SZ_2G + max_mappable, rte_size) +
303 round_up(vmemmap_size, rte_size) +
304 FIXMAP_SIZE + MODULES_LEN + KASLR_LEN;
305 if (IS_ENABLED(CONFIG_KMSAN))
306 vsize += MODULES_LEN * 2;
307 return size_add(vsize, vmalloc_size);
308 }
309
setup_kernel_memory_layout(unsigned long kernel_size)310 static unsigned long setup_kernel_memory_layout(unsigned long kernel_size)
311 {
312 unsigned long vmemmap_start;
313 unsigned long kernel_start;
314 unsigned long asce_limit;
315 unsigned long rte_size;
316 unsigned long pages;
317 unsigned long vsize;
318 unsigned long vmax;
319
320 pages = ident_map_size / PAGE_SIZE;
321 /* vmemmap contains a multiple of PAGES_PER_SECTION struct pages */
322 vmemmap_size = SECTION_ALIGN_UP(pages) * sizeof(struct page);
323
324 /* choose kernel address space layout: 4 or 3 levels. */
325 BUILD_BUG_ON(!IS_ALIGNED(TEXT_OFFSET, THREAD_SIZE));
326 BUILD_BUG_ON(!IS_ALIGNED(__NO_KASLR_START_KERNEL, THREAD_SIZE));
327 BUILD_BUG_ON(__NO_KASLR_END_KERNEL > _REGION1_SIZE);
328 vsize = get_vmem_size(ident_map_size, vmemmap_size, vmalloc_size, _REGION3_SIZE);
329 boot_debug("vmem size estimated: 0x%016lx\n", vsize);
330 if (IS_ENABLED(CONFIG_KASAN) || __NO_KASLR_END_KERNEL > _REGION2_SIZE ||
331 (vsize > _REGION2_SIZE && kaslr_enabled())) {
332 asce_limit = _REGION1_SIZE;
333 if (__NO_KASLR_END_KERNEL > _REGION2_SIZE) {
334 rte_size = _REGION2_SIZE;
335 vsize = get_vmem_size(ident_map_size, vmemmap_size, vmalloc_size, _REGION2_SIZE);
336 } else {
337 rte_size = _REGION3_SIZE;
338 }
339 } else {
340 asce_limit = _REGION2_SIZE;
341 rte_size = _REGION3_SIZE;
342 }
343
344 /*
345 * Forcing modules and vmalloc area under the ultravisor
346 * secure storage limit, so that any vmalloc allocation
347 * we do could be used to back secure guest storage.
348 *
349 * Assume the secure storage limit always exceeds _REGION2_SIZE,
350 * otherwise asce_limit and rte_size would have been adjusted.
351 */
352 vmax = adjust_to_uv_max(asce_limit);
353 boot_debug("%d level paging 0x%016lx vmax\n", vmax == _REGION1_SIZE ? 4 : 3, vmax);
354 #ifdef CONFIG_KASAN
355 BUILD_BUG_ON(__NO_KASLR_END_KERNEL > KASAN_SHADOW_START);
356 boot_debug("KASAN shadow area: 0x%016lx-0x%016lx\n", KASAN_SHADOW_START, KASAN_SHADOW_END);
357 /* force vmalloc and modules below kasan shadow */
358 vmax = min(vmax, KASAN_SHADOW_START);
359 #endif
360 vsize = min(vsize, vmax);
361 if (kaslr_enabled()) {
362 unsigned long kernel_end, kaslr_len, slots, pos;
363
364 kaslr_len = max(KASLR_LEN, vmax - vsize);
365 slots = DIV_ROUND_UP(kaslr_len - kernel_size, THREAD_SIZE);
366 if (get_random(slots, &pos))
367 pos = 0;
368 kernel_end = vmax - pos * THREAD_SIZE;
369 kernel_start = round_down(kernel_end - kernel_size, THREAD_SIZE);
370 boot_debug("Randomization range: 0x%016lx-0x%016lx\n", vmax - kaslr_len, vmax);
371 boot_debug("kernel image: 0x%016lx-0x%016lx (kaslr)\n", kernel_start,
372 kernel_size + kernel_size);
373 } else if (vmax < __NO_KASLR_END_KERNEL || vsize > __NO_KASLR_END_KERNEL) {
374 kernel_start = round_down(vmax - kernel_size, THREAD_SIZE);
375 boot_debug("kernel image: 0x%016lx-0x%016lx (constrained)\n", kernel_start,
376 kernel_start + kernel_size);
377 } else {
378 kernel_start = __NO_KASLR_START_KERNEL;
379 boot_debug("kernel image: 0x%016lx-0x%016lx (nokaslr)\n", kernel_start,
380 kernel_start + kernel_size);
381 }
382 __kaslr_offset = kernel_start;
383 boot_debug("__kaslr_offset: 0x%016lx\n", __kaslr_offset);
384
385 MODULES_END = round_down(kernel_start, _SEGMENT_SIZE);
386 MODULES_VADDR = MODULES_END - MODULES_LEN;
387 VMALLOC_END = MODULES_VADDR;
388 if (IS_ENABLED(CONFIG_KMSAN))
389 VMALLOC_END -= MODULES_LEN * 2;
390 boot_debug("modules area: 0x%016lx-0x%016lx\n", MODULES_VADDR, MODULES_END);
391
392 /* allow vmalloc area to occupy up to about 1/2 of the rest virtual space left */
393 vsize = (VMALLOC_END - FIXMAP_SIZE) / 2;
394 vsize = round_down(vsize, _SEGMENT_SIZE);
395 vmalloc_size = min(vmalloc_size, vsize);
396 if (IS_ENABLED(CONFIG_KMSAN)) {
397 /* take 2/3 of vmalloc area for KMSAN shadow and origins */
398 vmalloc_size = round_down(vmalloc_size / 3, _SEGMENT_SIZE);
399 VMALLOC_END -= vmalloc_size * 2;
400 }
401 VMALLOC_START = VMALLOC_END - vmalloc_size;
402 boot_debug("vmalloc area: 0x%016lx-0x%016lx\n", VMALLOC_START, VMALLOC_END);
403
404 __memcpy_real_area = round_down(VMALLOC_START - MEMCPY_REAL_SIZE, PAGE_SIZE);
405 boot_debug("memcpy real area: 0x%016lx-0x%016lx\n", __memcpy_real_area,
406 __memcpy_real_area + MEMCPY_REAL_SIZE);
407 __abs_lowcore = round_down(__memcpy_real_area - ABS_LOWCORE_MAP_SIZE,
408 sizeof(struct lowcore));
409 boot_debug("abs lowcore: 0x%016lx-0x%016lx\n", __abs_lowcore,
410 __abs_lowcore + ABS_LOWCORE_MAP_SIZE);
411
412 /* split remaining virtual space between 1:1 mapping & vmemmap array */
413 pages = __abs_lowcore / (PAGE_SIZE + sizeof(struct page));
414 pages = SECTION_ALIGN_UP(pages);
415 /* keep vmemmap_start aligned to a top level region table entry */
416 vmemmap_start = round_down(__abs_lowcore - pages * sizeof(struct page), rte_size);
417 /* make sure identity map doesn't overlay with vmemmap */
418 ident_map_size = min(ident_map_size, vmemmap_start);
419 vmemmap_size = SECTION_ALIGN_UP(ident_map_size / PAGE_SIZE) * sizeof(struct page);
420 /* make sure vmemmap doesn't overlay with absolute lowcore area */
421 if (vmemmap_start + vmemmap_size > __abs_lowcore) {
422 vmemmap_size = SECTION_ALIGN_DOWN(ident_map_size / PAGE_SIZE) * sizeof(struct page);
423 ident_map_size = vmemmap_size / sizeof(struct page) * PAGE_SIZE;
424 }
425 vmemmap = (struct page *)vmemmap_start;
426 /* maximum address for which linear mapping could be created (DCSS, memory) */
427 BUILD_BUG_ON(MAX_DCSS_ADDR > (1UL << MAX_PHYSMEM_BITS));
428 max_mappable = max(ident_map_size, MAX_DCSS_ADDR);
429 max_mappable = min(max_mappable, vmemmap_start);
430 #ifdef CONFIG_RANDOMIZE_IDENTITY_BASE
431 __identity_base = round_down(vmemmap_start - max_mappable, rte_size);
432 #endif
433 boot_debug("identity map: 0x%016lx-0x%016lx\n", __identity_base,
434 __identity_base + ident_map_size);
435
436 return asce_limit;
437 }
438
439 /*
440 * This function clears the BSS section of the decompressed Linux kernel and NOT the decompressor's.
441 */
clear_bss_section(unsigned long kernel_start)442 static void clear_bss_section(unsigned long kernel_start)
443 {
444 memset((void *)kernel_start + vmlinux.image_size, 0, vmlinux.bss_size);
445 }
446
447 /*
448 * Set vmalloc area size to an 8th of (potential) physical memory
449 * size, unless size has been set by kernel command line parameter.
450 */
setup_vmalloc_size(void)451 static void setup_vmalloc_size(void)
452 {
453 unsigned long size;
454
455 if (vmalloc_size_set)
456 return;
457 size = round_up(ident_map_size / 8, _SEGMENT_SIZE);
458 vmalloc_size = max(size, vmalloc_size);
459 }
460
kaslr_adjust_vmlinux_info(long offset)461 static void kaslr_adjust_vmlinux_info(long offset)
462 {
463 vmlinux.bootdata_off += offset;
464 vmlinux.bootdata_preserved_off += offset;
465 vmlinux.got_start += offset;
466 vmlinux.got_end += offset;
467 vmlinux.init_mm_off += offset;
468 vmlinux.swapper_pg_dir_off += offset;
469 vmlinux.invalid_pg_dir_off += offset;
470 vmlinux.alt_instructions += offset;
471 vmlinux.alt_instructions_end += offset;
472 #ifdef CONFIG_KASAN
473 vmlinux.kasan_early_shadow_page_off += offset;
474 vmlinux.kasan_early_shadow_pte_off += offset;
475 vmlinux.kasan_early_shadow_pmd_off += offset;
476 vmlinux.kasan_early_shadow_pud_off += offset;
477 vmlinux.kasan_early_shadow_p4d_off += offset;
478 #endif
479 }
480
startup_kernel(void)481 void startup_kernel(void)
482 {
483 unsigned long vmlinux_size = vmlinux.image_size + vmlinux.bss_size;
484 unsigned long nokaslr_text_lma, text_lma = 0, amode31_lma = 0;
485 unsigned long kernel_size = TEXT_OFFSET + vmlinux_size;
486 unsigned long kaslr_large_page_offset;
487 unsigned long max_physmem_end;
488 unsigned long asce_limit;
489 unsigned long safe_addr;
490 psw_t psw;
491
492 setup_lpp();
493 store_ipl_parmblock();
494 uv_query_info();
495 setup_boot_command_line();
496 parse_boot_command_line();
497
498 /*
499 * Non-randomized kernel physical start address must be _SEGMENT_SIZE
500 * aligned (see blow).
501 */
502 nokaslr_text_lma = ALIGN(mem_safe_offset(), _SEGMENT_SIZE);
503 safe_addr = PAGE_ALIGN(nokaslr_text_lma + vmlinux_size);
504
505 /*
506 * Reserve decompressor memory together with decompression heap,
507 * buffer and memory which might be occupied by uncompressed kernel
508 * (if KASLR is off or failed).
509 */
510 physmem_reserve(RR_DECOMPRESSOR, 0, safe_addr);
511 if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && parmarea.initrd_size)
512 physmem_reserve(RR_INITRD, parmarea.initrd_start, parmarea.initrd_size);
513 oldmem_data.start = parmarea.oldmem_base;
514 oldmem_data.size = parmarea.oldmem_size;
515
516 read_ipl_report();
517 sclp_early_read_info();
518 sclp_early_detect_machine_features();
519 detect_facilities();
520 detect_diag9c();
521 detect_machine_type();
522 cmma_init();
523 sanitize_prot_virt_host();
524 max_physmem_end = detect_max_physmem_end();
525 setup_ident_map_size(max_physmem_end);
526 setup_vmalloc_size();
527 asce_limit = setup_kernel_memory_layout(kernel_size);
528 /* got final ident_map_size, physmem allocations could be performed now */
529 physmem_set_usable_limit(ident_map_size);
530 detect_physmem_online_ranges(max_physmem_end);
531 save_ipl_cert_comp_list();
532 rescue_initrd(safe_addr, ident_map_size);
533
534 /*
535 * __kaslr_offset_phys must be _SEGMENT_SIZE aligned, so the lower
536 * 20 bits (the offset within a large page) are zero. Copy the last
537 * 20 bits of __kaslr_offset, which is THREAD_SIZE aligned, to
538 * __kaslr_offset_phys.
539 *
540 * With this the last 20 bits of __kaslr_offset_phys and __kaslr_offset
541 * are identical, which is required to allow for large mappings of the
542 * kernel image.
543 */
544 kaslr_large_page_offset = __kaslr_offset & ~_SEGMENT_MASK;
545 if (kaslr_enabled()) {
546 unsigned long size = vmlinux_size + kaslr_large_page_offset;
547
548 text_lma = randomize_within_range(size, _SEGMENT_SIZE, TEXT_OFFSET, ident_map_size);
549 }
550 if (!text_lma)
551 text_lma = nokaslr_text_lma;
552 text_lma |= kaslr_large_page_offset;
553
554 /*
555 * [__kaslr_offset_phys..__kaslr_offset_phys + TEXT_OFFSET] region is
556 * never accessed via the kernel image mapping as per the linker script:
557 *
558 * . = TEXT_OFFSET;
559 *
560 * Therefore, this region could be used for something else and does
561 * not need to be reserved. See how it is skipped in setup_vmem().
562 */
563 __kaslr_offset_phys = text_lma - TEXT_OFFSET;
564 kaslr_adjust_vmlinux_info(__kaslr_offset_phys);
565 physmem_reserve(RR_VMLINUX, text_lma, vmlinux_size);
566 deploy_kernel((void *)text_lma);
567
568 /* vmlinux decompression is done, shrink reserved low memory */
569 physmem_reserve(RR_DECOMPRESSOR, 0, (unsigned long)_decompressor_end);
570
571 /*
572 * In case KASLR is enabled the randomized location of .amode31
573 * section might overlap with .vmlinux.relocs section. To avoid that
574 * the below randomize_within_range() could have been called with
575 * __vmlinux_relocs_64_end as the lower range address. However,
576 * .amode31 section is written to by the decompressed kernel - at
577 * that time the contents of .vmlinux.relocs is not needed anymore.
578 * Conversely, .vmlinux.relocs is read only by the decompressor, even
579 * before the kernel started. Therefore, in case the two sections
580 * overlap there is no risk of corrupting any data.
581 */
582 if (kaslr_enabled()) {
583 unsigned long amode31_min;
584
585 amode31_min = (unsigned long)_decompressor_end;
586 amode31_lma = randomize_within_range(vmlinux.amode31_size, PAGE_SIZE, amode31_min, SZ_2G);
587 }
588 if (!amode31_lma)
589 amode31_lma = text_lma - vmlinux.amode31_size;
590 physmem_reserve(RR_AMODE31, amode31_lma, vmlinux.amode31_size);
591
592 /*
593 * The order of the following operations is important:
594 *
595 * - kaslr_adjust_relocs() must follow clear_bss_section() to establish
596 * static memory references to data in .bss to be used by setup_vmem()
597 * (i.e init_mm.pgd)
598 *
599 * - setup_vmem() must follow kaslr_adjust_relocs() to be able using
600 * static memory references to data in .bss (i.e init_mm.pgd)
601 *
602 * - copy_bootdata() must follow setup_vmem() to propagate changes
603 * to bootdata made by setup_vmem()
604 */
605 clear_bss_section(text_lma);
606 kaslr_adjust_relocs(text_lma, text_lma + vmlinux.image_size,
607 __kaslr_offset, __kaslr_offset_phys);
608 kaslr_adjust_got(__kaslr_offset);
609 setup_vmem(__kaslr_offset, __kaslr_offset + kernel_size, asce_limit);
610 dump_physmem_reserved();
611 copy_bootdata();
612 __apply_alternatives((struct alt_instr *)_vmlinux_info.alt_instructions,
613 (struct alt_instr *)_vmlinux_info.alt_instructions_end,
614 ALT_CTX_EARLY);
615
616 /*
617 * Save KASLR offset for early dumps, before vmcore_info is set.
618 * Mark as uneven to distinguish from real vmcore_info pointer.
619 */
620 get_lowcore()->vmcore_info = __kaslr_offset_phys ? __kaslr_offset_phys | 0x1UL : 0;
621
622 /*
623 * Jump to the decompressed kernel entry point and switch DAT mode on.
624 */
625 psw.addr = __kaslr_offset + vmlinux.entry;
626 psw.mask = PSW_KERNEL_BITS;
627 boot_debug("Starting kernel at: 0x%016lx\n", psw.addr);
628 __load_psw(psw);
629 }
630