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> 138f75582aSVasily Gorbik #include "compressed/decompressor.h" 148f75582aSVasily Gorbik #include "boot.h" 15c5cf5054SAlexander Egorenkov #include "uv.h" 168f75582aSVasily Gorbik 17b2d24b97SGerald Schaefer unsigned long __bootdata_preserved(__kaslr_offset); 180c4f2623SVasily Gorbik unsigned long __bootdata_preserved(VMALLOC_START); 190c4f2623SVasily Gorbik unsigned long __bootdata_preserved(VMALLOC_END); 200c4f2623SVasily Gorbik struct page *__bootdata_preserved(vmemmap); 210c4f2623SVasily Gorbik unsigned long __bootdata_preserved(vmemmap_size); 220c4f2623SVasily Gorbik unsigned long __bootdata_preserved(MODULES_VADDR); 230c4f2623SVasily Gorbik unsigned long __bootdata_preserved(MODULES_END); 2473045a08SVasily Gorbik unsigned long __bootdata(ident_map_size); 259f744abbSAlexander Egorenkov int __bootdata(is_full_image) = 1; 2684733284SAlexander Egorenkov struct initrd_data __bootdata(initrd_data); 27d1b52a43SVasily Gorbik 2817e89e13SSven Schnelle u64 __bootdata_preserved(stfle_fac_list[16]); 2917e89e13SSven Schnelle u64 __bootdata_preserved(alt_stfle_fac_list[16]); 30e9e7870fSAlexander Egorenkov struct oldmem_data __bootdata_preserved(oldmem_data); 3117e89e13SSven Schnelle 327516fc11SVasily Gorbik void error(char *x) 337516fc11SVasily Gorbik { 347516fc11SVasily Gorbik sclp_early_printk("\n\n"); 357516fc11SVasily Gorbik sclp_early_printk(x); 367516fc11SVasily Gorbik sclp_early_printk("\n\n -- System halted"); 377516fc11SVasily Gorbik 3898587c2dSMartin Schwidefsky disabled_wait(); 397516fc11SVasily Gorbik } 407516fc11SVasily Gorbik 41d7e7fbbaSVasily Gorbik static void setup_lpp(void) 42d7e7fbbaSVasily Gorbik { 43d7e7fbbaSVasily Gorbik S390_lowcore.current_pid = 0; 44d7e7fbbaSVasily Gorbik S390_lowcore.lpp = LPP_MAGIC; 45d7e7fbbaSVasily Gorbik if (test_facility(40)) 46d7e7fbbaSVasily Gorbik lpp(&S390_lowcore.lpp); 47d7e7fbbaSVasily Gorbik } 48d7e7fbbaSVasily Gorbik 4915426ca4SVasily Gorbik #ifdef CONFIG_KERNEL_UNCOMPRESSED 5015426ca4SVasily Gorbik unsigned long mem_safe_offset(void) 5115426ca4SVasily Gorbik { 5215426ca4SVasily Gorbik return vmlinux.default_lma + vmlinux.image_size + vmlinux.bss_size; 5315426ca4SVasily Gorbik } 5415426ca4SVasily Gorbik #endif 5515426ca4SVasily Gorbik 569641b8ccSMartin Schwidefsky static void rescue_initrd(unsigned long addr) 5715426ca4SVasily Gorbik { 5815426ca4SVasily Gorbik if (!IS_ENABLED(CONFIG_BLK_DEV_INITRD)) 5915426ca4SVasily Gorbik return; 6084733284SAlexander Egorenkov if (!initrd_data.start || !initrd_data.size) 6115426ca4SVasily Gorbik return; 6284733284SAlexander Egorenkov if (addr <= initrd_data.start) 6315426ca4SVasily Gorbik return; 6484733284SAlexander Egorenkov memmove((void *)addr, (void *)initrd_data.start, initrd_data.size); 6584733284SAlexander Egorenkov initrd_data.start = addr; 6615426ca4SVasily Gorbik } 6715426ca4SVasily Gorbik 68d1b52a43SVasily Gorbik static void copy_bootdata(void) 69d1b52a43SVasily Gorbik { 70d1b52a43SVasily Gorbik if (__boot_data_end - __boot_data_start != vmlinux.bootdata_size) 71d1b52a43SVasily Gorbik error(".boot.data section size mismatch"); 72d1b52a43SVasily Gorbik memcpy((void *)vmlinux.bootdata_off, __boot_data_start, vmlinux.bootdata_size); 73bf9921a9SGerald Schaefer if (__boot_data_preserved_end - __boot_data_preserved_start != vmlinux.bootdata_preserved_size) 74bf9921a9SGerald Schaefer error(".boot.preserved.data section size mismatch"); 75bf9921a9SGerald Schaefer memcpy((void *)vmlinux.bootdata_preserved_off, __boot_data_preserved_start, vmlinux.bootdata_preserved_size); 76d1b52a43SVasily Gorbik } 77d1b52a43SVasily Gorbik 78805bc0bcSGerald Schaefer static void handle_relocs(unsigned long offset) 79805bc0bcSGerald Schaefer { 80805bc0bcSGerald Schaefer Elf64_Rela *rela_start, *rela_end, *rela; 81805bc0bcSGerald Schaefer int r_type, r_sym, rc; 82805bc0bcSGerald Schaefer Elf64_Addr loc, val; 83805bc0bcSGerald Schaefer Elf64_Sym *dynsym; 84805bc0bcSGerald Schaefer 85805bc0bcSGerald Schaefer rela_start = (Elf64_Rela *) vmlinux.rela_dyn_start; 86805bc0bcSGerald Schaefer rela_end = (Elf64_Rela *) vmlinux.rela_dyn_end; 87805bc0bcSGerald Schaefer dynsym = (Elf64_Sym *) vmlinux.dynsym_start; 88805bc0bcSGerald Schaefer for (rela = rela_start; rela < rela_end; rela++) { 89805bc0bcSGerald Schaefer loc = rela->r_offset + offset; 90ac49303dSGerald Schaefer val = rela->r_addend; 91805bc0bcSGerald Schaefer r_sym = ELF64_R_SYM(rela->r_info); 92ac49303dSGerald Schaefer if (r_sym) { 93ac49303dSGerald Schaefer if (dynsym[r_sym].st_shndx != SHN_UNDEF) 94ac49303dSGerald Schaefer val += dynsym[r_sym].st_value + offset; 95ac49303dSGerald Schaefer } else { 96ac49303dSGerald Schaefer /* 97ac49303dSGerald Schaefer * 0 == undefined symbol table index (STN_UNDEF), 98ac49303dSGerald Schaefer * used for R_390_RELATIVE, only add KASLR offset 99ac49303dSGerald Schaefer */ 100ac49303dSGerald Schaefer val += offset; 101ac49303dSGerald Schaefer } 102805bc0bcSGerald Schaefer r_type = ELF64_R_TYPE(rela->r_info); 103805bc0bcSGerald Schaefer rc = arch_kexec_do_relocs(r_type, (void *) loc, val, 0); 104805bc0bcSGerald Schaefer if (rc) 105805bc0bcSGerald Schaefer error("Unknown relocation type"); 106805bc0bcSGerald Schaefer } 107805bc0bcSGerald Schaefer } 108805bc0bcSGerald Schaefer 109980d5f9aSAlexander Egorenkov /* 11073045a08SVasily Gorbik * Merge information from several sources into a single ident_map_size value. 11173045a08SVasily Gorbik * "ident_map_size" represents the upper limit of physical memory we may ever 11273045a08SVasily Gorbik * reach. It might not be all online memory, but also include standby (offline) 11373045a08SVasily Gorbik * memory. "ident_map_size" could be lower then actual standby or even online 11473045a08SVasily Gorbik * memory present, due to limiting factors. We should never go above this limit. 11573045a08SVasily Gorbik * It is the size of our identity mapping. 11673045a08SVasily Gorbik * 11773045a08SVasily Gorbik * Consider the following factors: 11873045a08SVasily Gorbik * 1. max_physmem_end - end of physical memory online or standby. 11973045a08SVasily Gorbik * Always <= end of the last online memory block (get_mem_detect_end()). 12073045a08SVasily Gorbik * 2. CONFIG_MAX_PHYSMEM_BITS - the maximum size of physical memory the 12173045a08SVasily Gorbik * kernel is able to support. 12273045a08SVasily Gorbik * 3. "mem=" kernel command line option which limits physical memory usage. 12373045a08SVasily Gorbik * 4. OLDMEM_BASE which is a kdump memory limit when the kernel is executed as 12473045a08SVasily Gorbik * crash kernel. 12573045a08SVasily Gorbik * 5. "hsa" size which is a memory limit when the kernel is executed during 12673045a08SVasily Gorbik * zfcp/nvme dump. 12773045a08SVasily Gorbik */ 12873045a08SVasily Gorbik static void setup_ident_map_size(unsigned long max_physmem_end) 12973045a08SVasily Gorbik { 13073045a08SVasily Gorbik unsigned long hsa_size; 13173045a08SVasily Gorbik 13273045a08SVasily Gorbik ident_map_size = max_physmem_end; 13373045a08SVasily Gorbik if (memory_limit) 13473045a08SVasily Gorbik ident_map_size = min(ident_map_size, memory_limit); 13573045a08SVasily Gorbik ident_map_size = min(ident_map_size, 1UL << MAX_PHYSMEM_BITS); 13673045a08SVasily Gorbik 13773045a08SVasily Gorbik #ifdef CONFIG_CRASH_DUMP 138e9e7870fSAlexander Egorenkov if (oldmem_data.start) { 13973045a08SVasily Gorbik kaslr_enabled = 0; 140e9e7870fSAlexander Egorenkov ident_map_size = min(ident_map_size, oldmem_data.size); 14173045a08SVasily Gorbik } else if (ipl_block_valid && is_ipl_block_dump()) { 14273045a08SVasily Gorbik kaslr_enabled = 0; 14373045a08SVasily Gorbik if (!sclp_early_get_hsa_size(&hsa_size) && hsa_size) 14473045a08SVasily Gorbik ident_map_size = min(ident_map_size, hsa_size); 14573045a08SVasily Gorbik } 14673045a08SVasily Gorbik #endif 14773045a08SVasily Gorbik } 14873045a08SVasily Gorbik 1490c4f2623SVasily Gorbik static void setup_kernel_memory_layout(void) 1500c4f2623SVasily Gorbik { 1510c4f2623SVasily Gorbik bool vmalloc_size_verified = false; 1520c4f2623SVasily Gorbik unsigned long vmemmap_off; 1530c4f2623SVasily Gorbik unsigned long vspace_left; 1540c4f2623SVasily Gorbik unsigned long rte_size; 1550c4f2623SVasily Gorbik unsigned long pages; 1560c4f2623SVasily Gorbik unsigned long vmax; 1570c4f2623SVasily Gorbik 1580c4f2623SVasily Gorbik pages = ident_map_size / PAGE_SIZE; 1590c4f2623SVasily Gorbik /* vmemmap contains a multiple of PAGES_PER_SECTION struct pages */ 1600c4f2623SVasily Gorbik vmemmap_size = SECTION_ALIGN_UP(pages) * sizeof(struct page); 1610c4f2623SVasily Gorbik 1620c4f2623SVasily Gorbik /* choose kernel address space layout: 4 or 3 levels. */ 1630c4f2623SVasily Gorbik vmemmap_off = round_up(ident_map_size, _REGION3_SIZE); 1640c4f2623SVasily Gorbik if (IS_ENABLED(CONFIG_KASAN) || 1650c4f2623SVasily Gorbik vmalloc_size > _REGION2_SIZE || 1660c4f2623SVasily Gorbik vmemmap_off + vmemmap_size + vmalloc_size + MODULES_LEN > _REGION2_SIZE) 1670c4f2623SVasily Gorbik vmax = _REGION1_SIZE; 1680c4f2623SVasily Gorbik else 1690c4f2623SVasily Gorbik vmax = _REGION2_SIZE; 1700c4f2623SVasily Gorbik 1710c4f2623SVasily Gorbik /* keep vmemmap_off aligned to a top level region table entry */ 1720c4f2623SVasily Gorbik rte_size = vmax == _REGION1_SIZE ? _REGION2_SIZE : _REGION3_SIZE; 1730c4f2623SVasily Gorbik MODULES_END = vmax; 1740c4f2623SVasily Gorbik if (is_prot_virt_host()) { 1750c4f2623SVasily Gorbik /* 1760c4f2623SVasily Gorbik * forcing modules and vmalloc area under the ultravisor 1770c4f2623SVasily Gorbik * secure storage limit, so that any vmalloc allocation 1780c4f2623SVasily Gorbik * we do could be used to back secure guest storage. 1790c4f2623SVasily Gorbik */ 1800c4f2623SVasily Gorbik adjust_to_uv_max(&MODULES_END); 1810c4f2623SVasily Gorbik } 1820c4f2623SVasily Gorbik 1830c4f2623SVasily Gorbik #ifdef CONFIG_KASAN 1840c4f2623SVasily Gorbik if (MODULES_END < vmax) { 1850c4f2623SVasily Gorbik /* force vmalloc and modules below kasan shadow */ 1860c4f2623SVasily Gorbik MODULES_END = min(MODULES_END, KASAN_SHADOW_START); 1870c4f2623SVasily Gorbik } else { 1880c4f2623SVasily Gorbik /* 1890c4f2623SVasily Gorbik * leave vmalloc and modules above kasan shadow but make 1900c4f2623SVasily Gorbik * sure they don't overlap with it 1910c4f2623SVasily Gorbik */ 1920c4f2623SVasily Gorbik vmalloc_size = min(vmalloc_size, vmax - KASAN_SHADOW_END - MODULES_LEN); 1930c4f2623SVasily Gorbik vmalloc_size_verified = true; 1940c4f2623SVasily Gorbik vspace_left = KASAN_SHADOW_START; 1950c4f2623SVasily Gorbik } 1960c4f2623SVasily Gorbik #endif 1970c4f2623SVasily Gorbik MODULES_VADDR = MODULES_END - MODULES_LEN; 1980c4f2623SVasily Gorbik VMALLOC_END = MODULES_VADDR; 1990c4f2623SVasily Gorbik 2000c4f2623SVasily Gorbik if (vmalloc_size_verified) { 2010c4f2623SVasily Gorbik VMALLOC_START = VMALLOC_END - vmalloc_size; 2020c4f2623SVasily Gorbik } else { 2030c4f2623SVasily Gorbik vmemmap_off = round_up(ident_map_size, rte_size); 2040c4f2623SVasily Gorbik 2050c4f2623SVasily Gorbik if (vmemmap_off + vmemmap_size > VMALLOC_END || 2060c4f2623SVasily Gorbik vmalloc_size > VMALLOC_END - vmemmap_off - vmemmap_size) { 2070c4f2623SVasily Gorbik /* 2080c4f2623SVasily Gorbik * allow vmalloc area to occupy up to 1/2 of 2090c4f2623SVasily Gorbik * the rest virtual space left. 2100c4f2623SVasily Gorbik */ 2110c4f2623SVasily Gorbik vmalloc_size = min(vmalloc_size, VMALLOC_END / 2); 2120c4f2623SVasily Gorbik } 2130c4f2623SVasily Gorbik VMALLOC_START = VMALLOC_END - vmalloc_size; 2140c4f2623SVasily Gorbik vspace_left = VMALLOC_START; 2150c4f2623SVasily Gorbik } 2160c4f2623SVasily Gorbik 2170c4f2623SVasily Gorbik pages = vspace_left / (PAGE_SIZE + sizeof(struct page)); 2180c4f2623SVasily Gorbik pages = SECTION_ALIGN_UP(pages); 2190c4f2623SVasily Gorbik vmemmap_off = round_up(vspace_left - pages * sizeof(struct page), rte_size); 2200c4f2623SVasily Gorbik /* keep vmemmap left most starting from a fresh region table entry */ 2210c4f2623SVasily Gorbik vmemmap_off = min(vmemmap_off, round_up(ident_map_size, rte_size)); 2220c4f2623SVasily Gorbik /* take care that identity map is lower then vmemmap */ 2230c4f2623SVasily Gorbik ident_map_size = min(ident_map_size, vmemmap_off); 2240c4f2623SVasily Gorbik vmemmap_size = SECTION_ALIGN_UP(ident_map_size / PAGE_SIZE) * sizeof(struct page); 2250c4f2623SVasily Gorbik VMALLOC_START = max(vmemmap_off + vmemmap_size, VMALLOC_START); 2260c4f2623SVasily Gorbik vmemmap = (struct page *)vmemmap_off; 2270c4f2623SVasily Gorbik } 2280c4f2623SVasily Gorbik 22973045a08SVasily Gorbik /* 230980d5f9aSAlexander Egorenkov * This function clears the BSS section of the decompressed Linux kernel and NOT the decompressor's. 231980d5f9aSAlexander Egorenkov */ 2322e83e0ebSVasily Gorbik static void clear_bss_section(void) 2332e83e0ebSVasily Gorbik { 2342e83e0ebSVasily Gorbik memset((void *)vmlinux.default_lma + vmlinux.image_size, 0, vmlinux.bss_size); 2352e83e0ebSVasily Gorbik } 2362e83e0ebSVasily Gorbik 23790178c19SHeiko Carstens /* 23890178c19SHeiko Carstens * Set vmalloc area size to an 8th of (potential) physical memory 23990178c19SHeiko Carstens * size, unless size has been set by kernel command line parameter. 24090178c19SHeiko Carstens */ 24190178c19SHeiko Carstens static void setup_vmalloc_size(void) 24290178c19SHeiko Carstens { 24390178c19SHeiko Carstens unsigned long size; 24490178c19SHeiko Carstens 24590178c19SHeiko Carstens if (vmalloc_size_set) 24690178c19SHeiko Carstens return; 24773045a08SVasily Gorbik size = round_up(ident_map_size / 8, _SEGMENT_SIZE); 24890178c19SHeiko Carstens vmalloc_size = max(size, vmalloc_size); 24990178c19SHeiko Carstens } 25090178c19SHeiko Carstens 251*e8f06683SAlexander Gordeev static void offset_vmlinux_info(unsigned long offset) 252*e8f06683SAlexander Gordeev { 253*e8f06683SAlexander Gordeev vmlinux.default_lma += offset; 254*e8f06683SAlexander Gordeev *(unsigned long *)(&vmlinux.entry) += offset; 255*e8f06683SAlexander Gordeev vmlinux.bootdata_off += offset; 256*e8f06683SAlexander Gordeev vmlinux.bootdata_preserved_off += offset; 257*e8f06683SAlexander Gordeev vmlinux.rela_dyn_start += offset; 258*e8f06683SAlexander Gordeev vmlinux.rela_dyn_end += offset; 259*e8f06683SAlexander Gordeev vmlinux.dynsym_start += offset; 260*e8f06683SAlexander Gordeev } 261*e8f06683SAlexander Gordeev 2628f75582aSVasily Gorbik void startup_kernel(void) 2638f75582aSVasily Gorbik { 264b2d24b97SGerald Schaefer unsigned long random_lma; 2659641b8ccSMartin Schwidefsky unsigned long safe_addr; 266369f91c3SVasily Gorbik void *img; 2678f75582aSVasily Gorbik 26884733284SAlexander Egorenkov initrd_data.start = parmarea.initrd_start; 26984733284SAlexander Egorenkov initrd_data.size = parmarea.initrd_size; 270e9e7870fSAlexander Egorenkov oldmem_data.start = parmarea.oldmem_base; 271e9e7870fSAlexander Egorenkov oldmem_data.size = parmarea.oldmem_size; 27284733284SAlexander Egorenkov 273d7e7fbbaSVasily Gorbik setup_lpp(); 27449698745SVasily Gorbik store_ipl_parmblock(); 2759641b8ccSMartin Schwidefsky safe_addr = mem_safe_offset(); 2769641b8ccSMartin Schwidefsky safe_addr = read_ipl_report(safe_addr); 2779641b8ccSMartin Schwidefsky uv_query_info(); 2789641b8ccSMartin Schwidefsky rescue_initrd(safe_addr); 2799641b8ccSMartin Schwidefsky sclp_early_read_info(); 28049698745SVasily Gorbik setup_boot_command_line(); 281b5e80459SVasily Gorbik parse_boot_command_line(); 28242c89439SAlexander Egorenkov sanitize_prot_virt_host(); 28373045a08SVasily Gorbik setup_ident_map_size(detect_memory()); 28490178c19SHeiko Carstens setup_vmalloc_size(); 2850c4f2623SVasily Gorbik setup_kernel_memory_layout(); 286b2d24b97SGerald Schaefer 287b2d24b97SGerald Schaefer if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && kaslr_enabled) { 288b2d24b97SGerald Schaefer random_lma = get_random_base(safe_addr); 289b2d24b97SGerald Schaefer if (random_lma) { 290b2d24b97SGerald Schaefer __kaslr_offset = random_lma - vmlinux.default_lma; 291b2d24b97SGerald Schaefer img = (void *)vmlinux.default_lma; 292*e8f06683SAlexander Gordeev offset_vmlinux_info(__kaslr_offset); 293b2d24b97SGerald Schaefer } 294b2d24b97SGerald Schaefer } 295b2d24b97SGerald Schaefer 2968f75582aSVasily Gorbik if (!IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED)) { 297369f91c3SVasily Gorbik img = decompress_kernel(); 298369f91c3SVasily Gorbik memmove((void *)vmlinux.default_lma, img, vmlinux.image_size); 299b2d24b97SGerald Schaefer } else if (__kaslr_offset) 300b2d24b97SGerald Schaefer memcpy((void *)vmlinux.default_lma, img, vmlinux.image_size); 301b2d24b97SGerald Schaefer 3022e83e0ebSVasily Gorbik clear_bss_section(); 303d1b52a43SVasily Gorbik copy_bootdata(); 304805bc0bcSGerald Schaefer if (IS_ENABLED(CONFIG_RELOCATABLE)) 305b2d24b97SGerald Schaefer handle_relocs(__kaslr_offset); 306b2d24b97SGerald Schaefer 307b2d24b97SGerald Schaefer if (__kaslr_offset) { 308a9f2f686SGerald Schaefer /* 309a9f2f686SGerald Schaefer * Save KASLR offset for early dumps, before vmcore_info is set. 310a9f2f686SGerald Schaefer * Mark as uneven to distinguish from real vmcore_info pointer. 311a9f2f686SGerald Schaefer */ 312a9f2f686SGerald Schaefer S390_lowcore.vmcore_info = __kaslr_offset | 0x1UL; 313b2d24b97SGerald Schaefer /* Clear non-relocated kernel */ 314b2d24b97SGerald Schaefer if (IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED)) 315b2d24b97SGerald Schaefer memset(img, 0, vmlinux.image_size); 316b2d24b97SGerald Schaefer } 317369f91c3SVasily Gorbik vmlinux.entry(); 3188f75582aSVasily Gorbik } 319