18f75582aSVasily Gorbik // SPDX-License-Identifier: GPL-2.0 28f75582aSVasily Gorbik #include <linux/string.h> 3805bc0bcSGerald Schaefer #include <linux/elf.h> 473045a08SVasily Gorbik #include <asm/boot_data.h> 5a80313ffSGerald Schaefer #include <asm/sections.h> 6d7e7fbbaSVasily Gorbik #include <asm/cpu_mf.h> 715426ca4SVasily Gorbik #include <asm/setup.h> 80c4f2623SVasily Gorbik #include <asm/kasan.h> 9805bc0bcSGerald Schaefer #include <asm/kexec.h> 107516fc11SVasily Gorbik #include <asm/sclp.h> 11a80313ffSGerald Schaefer #include <asm/diag.h> 125abb9351SVasily Gorbik #include <asm/uv.h> 134df29d2bSAlexander Gordeev #include <asm/abs_lowcore.h> 14edd4a866SHeiko Carstens #include "decompressor.h" 158f75582aSVasily Gorbik #include "boot.h" 16c5cf5054SAlexander Egorenkov #include "uv.h" 178f75582aSVasily Gorbik 18b2d24b97SGerald Schaefer unsigned long __bootdata_preserved(__kaslr_offset); 194df29d2bSAlexander Gordeev unsigned long __bootdata_preserved(__abs_lowcore); 20*2f0e8aaeSAlexander Gordeev unsigned long __bootdata_preserved(__memcpy_real_area); 21e3ec8e0fSAlexander Gordeev unsigned long __bootdata(__amode31_base); 220c4f2623SVasily Gorbik unsigned long __bootdata_preserved(VMALLOC_START); 230c4f2623SVasily Gorbik unsigned long __bootdata_preserved(VMALLOC_END); 240c4f2623SVasily Gorbik struct page *__bootdata_preserved(vmemmap); 250c4f2623SVasily Gorbik unsigned long __bootdata_preserved(vmemmap_size); 260c4f2623SVasily Gorbik unsigned long __bootdata_preserved(MODULES_VADDR); 270c4f2623SVasily Gorbik unsigned long __bootdata_preserved(MODULES_END); 2873045a08SVasily Gorbik unsigned long __bootdata(ident_map_size); 299f744abbSAlexander Egorenkov int __bootdata(is_full_image) = 1; 3084733284SAlexander Egorenkov struct initrd_data __bootdata(initrd_data); 31d1b52a43SVasily Gorbik 3217e89e13SSven Schnelle u64 __bootdata_preserved(stfle_fac_list[16]); 3317e89e13SSven Schnelle u64 __bootdata_preserved(alt_stfle_fac_list[16]); 34e9e7870fSAlexander Egorenkov struct oldmem_data __bootdata_preserved(oldmem_data); 3517e89e13SSven Schnelle 367516fc11SVasily Gorbik void error(char *x) 377516fc11SVasily Gorbik { 387516fc11SVasily Gorbik sclp_early_printk("\n\n"); 397516fc11SVasily Gorbik sclp_early_printk(x); 407516fc11SVasily Gorbik sclp_early_printk("\n\n -- System halted"); 417516fc11SVasily Gorbik 4298587c2dSMartin Schwidefsky disabled_wait(); 437516fc11SVasily Gorbik } 447516fc11SVasily Gorbik 45d7e7fbbaSVasily Gorbik static void setup_lpp(void) 46d7e7fbbaSVasily Gorbik { 47d7e7fbbaSVasily Gorbik S390_lowcore.current_pid = 0; 48d7e7fbbaSVasily Gorbik S390_lowcore.lpp = LPP_MAGIC; 49d7e7fbbaSVasily Gorbik if (test_facility(40)) 50d7e7fbbaSVasily Gorbik lpp(&S390_lowcore.lpp); 51d7e7fbbaSVasily Gorbik } 52d7e7fbbaSVasily Gorbik 5315426ca4SVasily Gorbik #ifdef CONFIG_KERNEL_UNCOMPRESSED 5415426ca4SVasily Gorbik unsigned long mem_safe_offset(void) 5515426ca4SVasily Gorbik { 5615426ca4SVasily Gorbik return vmlinux.default_lma + vmlinux.image_size + vmlinux.bss_size; 5715426ca4SVasily Gorbik } 5815426ca4SVasily Gorbik #endif 5915426ca4SVasily Gorbik 609641b8ccSMartin Schwidefsky static void rescue_initrd(unsigned long addr) 6115426ca4SVasily Gorbik { 6215426ca4SVasily Gorbik if (!IS_ENABLED(CONFIG_BLK_DEV_INITRD)) 6315426ca4SVasily Gorbik return; 6484733284SAlexander Egorenkov if (!initrd_data.start || !initrd_data.size) 6515426ca4SVasily Gorbik return; 6684733284SAlexander Egorenkov if (addr <= initrd_data.start) 6715426ca4SVasily Gorbik return; 6884733284SAlexander Egorenkov memmove((void *)addr, (void *)initrd_data.start, initrd_data.size); 6984733284SAlexander Egorenkov initrd_data.start = addr; 7015426ca4SVasily Gorbik } 7115426ca4SVasily Gorbik 72d1b52a43SVasily Gorbik static void copy_bootdata(void) 73d1b52a43SVasily Gorbik { 74d1b52a43SVasily Gorbik if (__boot_data_end - __boot_data_start != vmlinux.bootdata_size) 75d1b52a43SVasily Gorbik error(".boot.data section size mismatch"); 76d1b52a43SVasily Gorbik memcpy((void *)vmlinux.bootdata_off, __boot_data_start, vmlinux.bootdata_size); 77bf9921a9SGerald Schaefer if (__boot_data_preserved_end - __boot_data_preserved_start != vmlinux.bootdata_preserved_size) 78bf9921a9SGerald Schaefer error(".boot.preserved.data section size mismatch"); 79bf9921a9SGerald Schaefer memcpy((void *)vmlinux.bootdata_preserved_off, __boot_data_preserved_start, vmlinux.bootdata_preserved_size); 80d1b52a43SVasily Gorbik } 81d1b52a43SVasily Gorbik 82805bc0bcSGerald Schaefer static void handle_relocs(unsigned long offset) 83805bc0bcSGerald Schaefer { 84805bc0bcSGerald Schaefer Elf64_Rela *rela_start, *rela_end, *rela; 85805bc0bcSGerald Schaefer int r_type, r_sym, rc; 86805bc0bcSGerald Schaefer Elf64_Addr loc, val; 87805bc0bcSGerald Schaefer Elf64_Sym *dynsym; 88805bc0bcSGerald Schaefer 89805bc0bcSGerald Schaefer rela_start = (Elf64_Rela *) vmlinux.rela_dyn_start; 90805bc0bcSGerald Schaefer rela_end = (Elf64_Rela *) vmlinux.rela_dyn_end; 91805bc0bcSGerald Schaefer dynsym = (Elf64_Sym *) vmlinux.dynsym_start; 92805bc0bcSGerald Schaefer for (rela = rela_start; rela < rela_end; rela++) { 93805bc0bcSGerald Schaefer loc = rela->r_offset + offset; 94ac49303dSGerald Schaefer val = rela->r_addend; 95805bc0bcSGerald Schaefer r_sym = ELF64_R_SYM(rela->r_info); 96ac49303dSGerald Schaefer if (r_sym) { 97ac49303dSGerald Schaefer if (dynsym[r_sym].st_shndx != SHN_UNDEF) 98ac49303dSGerald Schaefer val += dynsym[r_sym].st_value + offset; 99ac49303dSGerald Schaefer } else { 100ac49303dSGerald Schaefer /* 101ac49303dSGerald Schaefer * 0 == undefined symbol table index (STN_UNDEF), 102ac49303dSGerald Schaefer * used for R_390_RELATIVE, only add KASLR offset 103ac49303dSGerald Schaefer */ 104ac49303dSGerald Schaefer val += offset; 105ac49303dSGerald Schaefer } 106805bc0bcSGerald Schaefer r_type = ELF64_R_TYPE(rela->r_info); 107805bc0bcSGerald Schaefer rc = arch_kexec_do_relocs(r_type, (void *) loc, val, 0); 108805bc0bcSGerald Schaefer if (rc) 109805bc0bcSGerald Schaefer error("Unknown relocation type"); 110805bc0bcSGerald Schaefer } 111805bc0bcSGerald Schaefer } 112805bc0bcSGerald Schaefer 113980d5f9aSAlexander Egorenkov /* 11473045a08SVasily Gorbik * Merge information from several sources into a single ident_map_size value. 11573045a08SVasily Gorbik * "ident_map_size" represents the upper limit of physical memory we may ever 11673045a08SVasily Gorbik * reach. It might not be all online memory, but also include standby (offline) 11773045a08SVasily Gorbik * memory. "ident_map_size" could be lower then actual standby or even online 11873045a08SVasily Gorbik * memory present, due to limiting factors. We should never go above this limit. 11973045a08SVasily Gorbik * It is the size of our identity mapping. 12073045a08SVasily Gorbik * 12173045a08SVasily Gorbik * Consider the following factors: 12273045a08SVasily Gorbik * 1. max_physmem_end - end of physical memory online or standby. 12373045a08SVasily Gorbik * Always <= end of the last online memory block (get_mem_detect_end()). 12473045a08SVasily Gorbik * 2. CONFIG_MAX_PHYSMEM_BITS - the maximum size of physical memory the 12573045a08SVasily Gorbik * kernel is able to support. 12673045a08SVasily Gorbik * 3. "mem=" kernel command line option which limits physical memory usage. 12773045a08SVasily Gorbik * 4. OLDMEM_BASE which is a kdump memory limit when the kernel is executed as 12873045a08SVasily Gorbik * crash kernel. 12973045a08SVasily Gorbik * 5. "hsa" size which is a memory limit when the kernel is executed during 13073045a08SVasily Gorbik * zfcp/nvme dump. 13173045a08SVasily Gorbik */ 13273045a08SVasily Gorbik static void setup_ident_map_size(unsigned long max_physmem_end) 13373045a08SVasily Gorbik { 13473045a08SVasily Gorbik unsigned long hsa_size; 13573045a08SVasily Gorbik 13673045a08SVasily Gorbik ident_map_size = max_physmem_end; 13773045a08SVasily Gorbik if (memory_limit) 13873045a08SVasily Gorbik ident_map_size = min(ident_map_size, memory_limit); 13973045a08SVasily Gorbik ident_map_size = min(ident_map_size, 1UL << MAX_PHYSMEM_BITS); 14073045a08SVasily Gorbik 14173045a08SVasily Gorbik #ifdef CONFIG_CRASH_DUMP 142e9e7870fSAlexander Egorenkov if (oldmem_data.start) { 14373045a08SVasily Gorbik kaslr_enabled = 0; 144e9e7870fSAlexander Egorenkov ident_map_size = min(ident_map_size, oldmem_data.size); 14573045a08SVasily Gorbik } else if (ipl_block_valid && is_ipl_block_dump()) { 14673045a08SVasily Gorbik kaslr_enabled = 0; 14773045a08SVasily Gorbik if (!sclp_early_get_hsa_size(&hsa_size) && hsa_size) 14873045a08SVasily Gorbik ident_map_size = min(ident_map_size, hsa_size); 14973045a08SVasily Gorbik } 15073045a08SVasily Gorbik #endif 15173045a08SVasily Gorbik } 15273045a08SVasily Gorbik 1530c4f2623SVasily Gorbik static void setup_kernel_memory_layout(void) 1540c4f2623SVasily Gorbik { 1559a39abb7SVasily Gorbik unsigned long vmemmap_start; 1560c4f2623SVasily Gorbik unsigned long rte_size; 1570c4f2623SVasily Gorbik unsigned long pages; 15857ad19bcSAlexander Gordeev unsigned long vmax; 1590c4f2623SVasily Gorbik 1600c4f2623SVasily Gorbik pages = ident_map_size / PAGE_SIZE; 1610c4f2623SVasily Gorbik /* vmemmap contains a multiple of PAGES_PER_SECTION struct pages */ 1620c4f2623SVasily Gorbik vmemmap_size = SECTION_ALIGN_UP(pages) * sizeof(struct page); 1630c4f2623SVasily Gorbik 1640c4f2623SVasily Gorbik /* choose kernel address space layout: 4 or 3 levels. */ 1659a39abb7SVasily Gorbik vmemmap_start = round_up(ident_map_size, _REGION3_SIZE); 1660c4f2623SVasily Gorbik if (IS_ENABLED(CONFIG_KASAN) || 1670c4f2623SVasily Gorbik vmalloc_size > _REGION2_SIZE || 1689a39abb7SVasily Gorbik vmemmap_start + vmemmap_size + vmalloc_size + MODULES_LEN > 1699a39abb7SVasily Gorbik _REGION2_SIZE) { 17057ad19bcSAlexander Gordeev vmax = _REGION1_SIZE; 1719a39abb7SVasily Gorbik rte_size = _REGION2_SIZE; 1729a39abb7SVasily Gorbik } else { 17357ad19bcSAlexander Gordeev vmax = _REGION2_SIZE; 1749a39abb7SVasily Gorbik rte_size = _REGION3_SIZE; 1759a39abb7SVasily Gorbik } 1760c4f2623SVasily Gorbik /* 1770c4f2623SVasily Gorbik * forcing modules and vmalloc area under the ultravisor 1780c4f2623SVasily Gorbik * secure storage limit, so that any vmalloc allocation 1790c4f2623SVasily Gorbik * we do could be used to back secure guest storage. 1800c4f2623SVasily Gorbik */ 18157ad19bcSAlexander Gordeev vmax = adjust_to_uv_max(vmax); 1820c4f2623SVasily Gorbik #ifdef CONFIG_KASAN 1830c4f2623SVasily Gorbik /* force vmalloc and modules below kasan shadow */ 18457ad19bcSAlexander Gordeev vmax = min(vmax, KASAN_SHADOW_START); 1850c4f2623SVasily Gorbik #endif 186*2f0e8aaeSAlexander Gordeev __memcpy_real_area = round_down(vmax - PAGE_SIZE, PAGE_SIZE); 187*2f0e8aaeSAlexander Gordeev __abs_lowcore = round_down(__memcpy_real_area - ABS_LOWCORE_MAP_SIZE, 188*2f0e8aaeSAlexander Gordeev sizeof(struct lowcore)); 1894df29d2bSAlexander Gordeev MODULES_END = round_down(__abs_lowcore, _SEGMENT_SIZE); 1900c4f2623SVasily Gorbik MODULES_VADDR = MODULES_END - MODULES_LEN; 1910c4f2623SVasily Gorbik VMALLOC_END = MODULES_VADDR; 1920c4f2623SVasily Gorbik 1939a39abb7SVasily Gorbik /* allow vmalloc area to occupy up to about 1/2 of the rest virtual space left */ 1949a39abb7SVasily Gorbik vmalloc_size = min(vmalloc_size, round_down(VMALLOC_END / 2, _REGION3_SIZE)); 1950c4f2623SVasily Gorbik VMALLOC_START = VMALLOC_END - vmalloc_size; 1960c4f2623SVasily Gorbik 1979a39abb7SVasily Gorbik /* split remaining virtual space between 1:1 mapping & vmemmap array */ 1989a39abb7SVasily Gorbik pages = VMALLOC_START / (PAGE_SIZE + sizeof(struct page)); 1990c4f2623SVasily Gorbik pages = SECTION_ALIGN_UP(pages); 2009a39abb7SVasily Gorbik /* keep vmemmap_start aligned to a top level region table entry */ 2019a39abb7SVasily Gorbik vmemmap_start = round_down(VMALLOC_START - pages * sizeof(struct page), rte_size); 2029a39abb7SVasily Gorbik /* vmemmap_start is the future VMEM_MAX_PHYS, make sure it is within MAX_PHYSMEM */ 2039a39abb7SVasily Gorbik vmemmap_start = min(vmemmap_start, 1UL << MAX_PHYSMEM_BITS); 2049a39abb7SVasily Gorbik /* make sure identity map doesn't overlay with vmemmap */ 2059a39abb7SVasily Gorbik ident_map_size = min(ident_map_size, vmemmap_start); 2060c4f2623SVasily Gorbik vmemmap_size = SECTION_ALIGN_UP(ident_map_size / PAGE_SIZE) * sizeof(struct page); 2079a39abb7SVasily Gorbik /* make sure vmemmap doesn't overlay with vmalloc area */ 2089a39abb7SVasily Gorbik VMALLOC_START = max(vmemmap_start + vmemmap_size, VMALLOC_START); 2099a39abb7SVasily Gorbik vmemmap = (struct page *)vmemmap_start; 2100c4f2623SVasily Gorbik } 2110c4f2623SVasily Gorbik 21273045a08SVasily Gorbik /* 213980d5f9aSAlexander Egorenkov * This function clears the BSS section of the decompressed Linux kernel and NOT the decompressor's. 214980d5f9aSAlexander Egorenkov */ 2152e83e0ebSVasily Gorbik static void clear_bss_section(void) 2162e83e0ebSVasily Gorbik { 2172e83e0ebSVasily Gorbik memset((void *)vmlinux.default_lma + vmlinux.image_size, 0, vmlinux.bss_size); 2182e83e0ebSVasily Gorbik } 2192e83e0ebSVasily Gorbik 22090178c19SHeiko Carstens /* 22190178c19SHeiko Carstens * Set vmalloc area size to an 8th of (potential) physical memory 22290178c19SHeiko Carstens * size, unless size has been set by kernel command line parameter. 22390178c19SHeiko Carstens */ 22490178c19SHeiko Carstens static void setup_vmalloc_size(void) 22590178c19SHeiko Carstens { 22690178c19SHeiko Carstens unsigned long size; 22790178c19SHeiko Carstens 22890178c19SHeiko Carstens if (vmalloc_size_set) 22990178c19SHeiko Carstens return; 23073045a08SVasily Gorbik size = round_up(ident_map_size / 8, _SEGMENT_SIZE); 23190178c19SHeiko Carstens vmalloc_size = max(size, vmalloc_size); 23290178c19SHeiko Carstens } 23390178c19SHeiko Carstens 234e8f06683SAlexander Gordeev static void offset_vmlinux_info(unsigned long offset) 235e8f06683SAlexander Gordeev { 236e8f06683SAlexander Gordeev vmlinux.default_lma += offset; 237e8f06683SAlexander Gordeev *(unsigned long *)(&vmlinux.entry) += offset; 238e8f06683SAlexander Gordeev vmlinux.bootdata_off += offset; 239e8f06683SAlexander Gordeev vmlinux.bootdata_preserved_off += offset; 240e8f06683SAlexander Gordeev vmlinux.rela_dyn_start += offset; 241e8f06683SAlexander Gordeev vmlinux.rela_dyn_end += offset; 242e8f06683SAlexander Gordeev vmlinux.dynsym_start += offset; 243e8f06683SAlexander Gordeev } 244e8f06683SAlexander Gordeev 245e3ec8e0fSAlexander Gordeev static unsigned long reserve_amode31(unsigned long safe_addr) 246e3ec8e0fSAlexander Gordeev { 247e3ec8e0fSAlexander Gordeev __amode31_base = PAGE_ALIGN(safe_addr); 248e3ec8e0fSAlexander Gordeev return safe_addr + vmlinux.amode31_size; 249e3ec8e0fSAlexander Gordeev } 250e3ec8e0fSAlexander Gordeev 2518f75582aSVasily Gorbik void startup_kernel(void) 2528f75582aSVasily Gorbik { 253b2d24b97SGerald Schaefer unsigned long random_lma; 2549641b8ccSMartin Schwidefsky unsigned long safe_addr; 255369f91c3SVasily Gorbik void *img; 2568f75582aSVasily Gorbik 25784733284SAlexander Egorenkov initrd_data.start = parmarea.initrd_start; 25884733284SAlexander Egorenkov initrd_data.size = parmarea.initrd_size; 259e9e7870fSAlexander Egorenkov oldmem_data.start = parmarea.oldmem_base; 260e9e7870fSAlexander Egorenkov oldmem_data.size = parmarea.oldmem_size; 26184733284SAlexander Egorenkov 262d7e7fbbaSVasily Gorbik setup_lpp(); 26349698745SVasily Gorbik store_ipl_parmblock(); 2649641b8ccSMartin Schwidefsky safe_addr = mem_safe_offset(); 265e3ec8e0fSAlexander Gordeev safe_addr = reserve_amode31(safe_addr); 2669641b8ccSMartin Schwidefsky safe_addr = read_ipl_report(safe_addr); 2679641b8ccSMartin Schwidefsky uv_query_info(); 2689641b8ccSMartin Schwidefsky rescue_initrd(safe_addr); 2699641b8ccSMartin Schwidefsky sclp_early_read_info(); 27049698745SVasily Gorbik setup_boot_command_line(); 271b5e80459SVasily Gorbik parse_boot_command_line(); 27242c89439SAlexander Egorenkov sanitize_prot_virt_host(); 27373045a08SVasily Gorbik setup_ident_map_size(detect_memory()); 27490178c19SHeiko Carstens setup_vmalloc_size(); 2750c4f2623SVasily Gorbik setup_kernel_memory_layout(); 276b2d24b97SGerald Schaefer 277b2d24b97SGerald Schaefer if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && kaslr_enabled) { 278b2d24b97SGerald Schaefer random_lma = get_random_base(safe_addr); 279b2d24b97SGerald Schaefer if (random_lma) { 280b2d24b97SGerald Schaefer __kaslr_offset = random_lma - vmlinux.default_lma; 281b2d24b97SGerald Schaefer img = (void *)vmlinux.default_lma; 282e8f06683SAlexander Gordeev offset_vmlinux_info(__kaslr_offset); 283b2d24b97SGerald Schaefer } 284b2d24b97SGerald Schaefer } 285b2d24b97SGerald Schaefer 2868f75582aSVasily Gorbik if (!IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED)) { 287369f91c3SVasily Gorbik img = decompress_kernel(); 288369f91c3SVasily Gorbik memmove((void *)vmlinux.default_lma, img, vmlinux.image_size); 289b2d24b97SGerald Schaefer } else if (__kaslr_offset) 290b2d24b97SGerald Schaefer memcpy((void *)vmlinux.default_lma, img, vmlinux.image_size); 291b2d24b97SGerald Schaefer 2922e83e0ebSVasily Gorbik clear_bss_section(); 293d1b52a43SVasily Gorbik copy_bootdata(); 294805bc0bcSGerald Schaefer if (IS_ENABLED(CONFIG_RELOCATABLE)) 295b2d24b97SGerald Schaefer handle_relocs(__kaslr_offset); 296b2d24b97SGerald Schaefer 297b2d24b97SGerald Schaefer if (__kaslr_offset) { 298a9f2f686SGerald Schaefer /* 299a9f2f686SGerald Schaefer * Save KASLR offset for early dumps, before vmcore_info is set. 300a9f2f686SGerald Schaefer * Mark as uneven to distinguish from real vmcore_info pointer. 301a9f2f686SGerald Schaefer */ 302a9f2f686SGerald Schaefer S390_lowcore.vmcore_info = __kaslr_offset | 0x1UL; 303b2d24b97SGerald Schaefer /* Clear non-relocated kernel */ 304b2d24b97SGerald Schaefer if (IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED)) 305b2d24b97SGerald Schaefer memset(img, 0, vmlinux.image_size); 306b2d24b97SGerald Schaefer } 307369f91c3SVasily Gorbik vmlinux.entry(); 3088f75582aSVasily Gorbik } 309