18f75582aSVasily Gorbik // SPDX-License-Identifier: GPL-2.0 28f75582aSVasily Gorbik #include <linux/string.h> 3805bc0bcSGerald Schaefer #include <linux/elf.h> 4a80313ffSGerald Schaefer #include <asm/sections.h> 515426ca4SVasily Gorbik #include <asm/setup.h> 6805bc0bcSGerald Schaefer #include <asm/kexec.h> 77516fc11SVasily Gorbik #include <asm/sclp.h> 8a80313ffSGerald Schaefer #include <asm/diag.h> 95abb9351SVasily Gorbik #include <asm/uv.h> 108f75582aSVasily Gorbik #include "compressed/decompressor.h" 118f75582aSVasily Gorbik #include "boot.h" 128f75582aSVasily Gorbik 13d1b52a43SVasily Gorbik extern char __boot_data_start[], __boot_data_end[]; 14bf9921a9SGerald Schaefer extern char __boot_data_preserved_start[], __boot_data_preserved_end[]; 15b2d24b97SGerald Schaefer unsigned long __bootdata_preserved(__kaslr_offset); 16d1b52a43SVasily Gorbik 17a80313ffSGerald Schaefer /* 18a80313ffSGerald Schaefer * Some code and data needs to stay below 2 GB, even when the kernel would be 19a80313ffSGerald Schaefer * relocated above 2 GB, because it has to use 31 bit addresses. 20a80313ffSGerald Schaefer * Such code and data is part of the .dma section, and its location is passed 21a80313ffSGerald Schaefer * over to the decompressed / relocated kernel via the .boot.preserved.data 22a80313ffSGerald Schaefer * section. 23a80313ffSGerald Schaefer */ 24a80313ffSGerald Schaefer extern char _sdma[], _edma[]; 25a80313ffSGerald Schaefer extern char _stext_dma[], _etext_dma[]; 26a80313ffSGerald Schaefer extern struct exception_table_entry _start_dma_ex_table[]; 27a80313ffSGerald Schaefer extern struct exception_table_entry _stop_dma_ex_table[]; 28a80313ffSGerald Schaefer unsigned long __bootdata_preserved(__sdma) = __pa(&_sdma); 29a80313ffSGerald Schaefer unsigned long __bootdata_preserved(__edma) = __pa(&_edma); 30a80313ffSGerald Schaefer unsigned long __bootdata_preserved(__stext_dma) = __pa(&_stext_dma); 31a80313ffSGerald Schaefer unsigned long __bootdata_preserved(__etext_dma) = __pa(&_etext_dma); 32a80313ffSGerald Schaefer struct exception_table_entry * 33a80313ffSGerald Schaefer __bootdata_preserved(__start_dma_ex_table) = _start_dma_ex_table; 34a80313ffSGerald Schaefer struct exception_table_entry * 35a80313ffSGerald Schaefer __bootdata_preserved(__stop_dma_ex_table) = _stop_dma_ex_table; 36a80313ffSGerald Schaefer 37a80313ffSGerald Schaefer int _diag210_dma(struct diag210 *addr); 38a80313ffSGerald Schaefer int _diag26c_dma(void *req, void *resp, enum diag26c_sc subcode); 39a80313ffSGerald Schaefer int _diag14_dma(unsigned long rx, unsigned long ry1, unsigned long subcode); 40a80313ffSGerald Schaefer void _diag0c_dma(struct hypfs_diag0c_entry *entry); 41a80313ffSGerald Schaefer void _diag308_reset_dma(void); 42a80313ffSGerald Schaefer struct diag_ops __bootdata_preserved(diag_dma_ops) = { 43a80313ffSGerald Schaefer .diag210 = _diag210_dma, 44a80313ffSGerald Schaefer .diag26c = _diag26c_dma, 45a80313ffSGerald Schaefer .diag14 = _diag14_dma, 46a80313ffSGerald Schaefer .diag0c = _diag0c_dma, 47a80313ffSGerald Schaefer .diag308_reset = _diag308_reset_dma 48a80313ffSGerald Schaefer }; 494f84b383SNick Desaulniers static struct diag210 _diag210_tmp_dma __section(.dma.data); 50a80313ffSGerald Schaefer struct diag210 *__bootdata_preserved(__diag210_tmp_dma) = &_diag210_tmp_dma; 51a80313ffSGerald Schaefer void _swsusp_reset_dma(void); 52a80313ffSGerald Schaefer unsigned long __bootdata_preserved(__swsusp_reset_dma) = __pa(_swsusp_reset_dma); 53a80313ffSGerald Schaefer 547516fc11SVasily Gorbik void error(char *x) 557516fc11SVasily Gorbik { 567516fc11SVasily Gorbik sclp_early_printk("\n\n"); 577516fc11SVasily Gorbik sclp_early_printk(x); 587516fc11SVasily Gorbik sclp_early_printk("\n\n -- System halted"); 597516fc11SVasily Gorbik 6098587c2dSMartin Schwidefsky disabled_wait(); 617516fc11SVasily Gorbik } 627516fc11SVasily Gorbik 6315426ca4SVasily Gorbik #ifdef CONFIG_KERNEL_UNCOMPRESSED 6415426ca4SVasily Gorbik unsigned long mem_safe_offset(void) 6515426ca4SVasily Gorbik { 6615426ca4SVasily Gorbik return vmlinux.default_lma + vmlinux.image_size + vmlinux.bss_size; 6715426ca4SVasily Gorbik } 6815426ca4SVasily Gorbik #endif 6915426ca4SVasily Gorbik 709641b8ccSMartin Schwidefsky static void rescue_initrd(unsigned long addr) 7115426ca4SVasily Gorbik { 7215426ca4SVasily Gorbik if (!IS_ENABLED(CONFIG_BLK_DEV_INITRD)) 7315426ca4SVasily Gorbik return; 7415426ca4SVasily Gorbik if (!INITRD_START || !INITRD_SIZE) 7515426ca4SVasily Gorbik return; 769641b8ccSMartin Schwidefsky if (addr <= INITRD_START) 7715426ca4SVasily Gorbik return; 789641b8ccSMartin Schwidefsky memmove((void *)addr, (void *)INITRD_START, INITRD_SIZE); 799641b8ccSMartin Schwidefsky INITRD_START = addr; 8015426ca4SVasily Gorbik } 8115426ca4SVasily Gorbik 82d1b52a43SVasily Gorbik static void copy_bootdata(void) 83d1b52a43SVasily Gorbik { 84d1b52a43SVasily Gorbik if (__boot_data_end - __boot_data_start != vmlinux.bootdata_size) 85d1b52a43SVasily Gorbik error(".boot.data section size mismatch"); 86d1b52a43SVasily Gorbik memcpy((void *)vmlinux.bootdata_off, __boot_data_start, vmlinux.bootdata_size); 87bf9921a9SGerald Schaefer if (__boot_data_preserved_end - __boot_data_preserved_start != vmlinux.bootdata_preserved_size) 88bf9921a9SGerald Schaefer error(".boot.preserved.data section size mismatch"); 89bf9921a9SGerald Schaefer memcpy((void *)vmlinux.bootdata_preserved_off, __boot_data_preserved_start, vmlinux.bootdata_preserved_size); 90d1b52a43SVasily Gorbik } 91d1b52a43SVasily Gorbik 92805bc0bcSGerald Schaefer static void handle_relocs(unsigned long offset) 93805bc0bcSGerald Schaefer { 94805bc0bcSGerald Schaefer Elf64_Rela *rela_start, *rela_end, *rela; 95805bc0bcSGerald Schaefer int r_type, r_sym, rc; 96805bc0bcSGerald Schaefer Elf64_Addr loc, val; 97805bc0bcSGerald Schaefer Elf64_Sym *dynsym; 98805bc0bcSGerald Schaefer 99805bc0bcSGerald Schaefer rela_start = (Elf64_Rela *) vmlinux.rela_dyn_start; 100805bc0bcSGerald Schaefer rela_end = (Elf64_Rela *) vmlinux.rela_dyn_end; 101805bc0bcSGerald Schaefer dynsym = (Elf64_Sym *) vmlinux.dynsym_start; 102805bc0bcSGerald Schaefer for (rela = rela_start; rela < rela_end; rela++) { 103805bc0bcSGerald Schaefer loc = rela->r_offset + offset; 104ac49303dSGerald Schaefer val = rela->r_addend; 105805bc0bcSGerald Schaefer r_sym = ELF64_R_SYM(rela->r_info); 106ac49303dSGerald Schaefer if (r_sym) { 107ac49303dSGerald Schaefer if (dynsym[r_sym].st_shndx != SHN_UNDEF) 108ac49303dSGerald Schaefer val += dynsym[r_sym].st_value + offset; 109ac49303dSGerald Schaefer } else { 110ac49303dSGerald Schaefer /* 111ac49303dSGerald Schaefer * 0 == undefined symbol table index (STN_UNDEF), 112ac49303dSGerald Schaefer * used for R_390_RELATIVE, only add KASLR offset 113ac49303dSGerald Schaefer */ 114ac49303dSGerald Schaefer val += offset; 115ac49303dSGerald Schaefer } 116805bc0bcSGerald Schaefer r_type = ELF64_R_TYPE(rela->r_info); 117805bc0bcSGerald Schaefer rc = arch_kexec_do_relocs(r_type, (void *) loc, val, 0); 118805bc0bcSGerald Schaefer if (rc) 119805bc0bcSGerald Schaefer error("Unknown relocation type"); 120805bc0bcSGerald Schaefer } 121805bc0bcSGerald Schaefer } 122805bc0bcSGerald Schaefer 1232e83e0ebSVasily Gorbik static void clear_bss_section(void) 1242e83e0ebSVasily Gorbik { 1252e83e0ebSVasily Gorbik memset((void *)vmlinux.default_lma + vmlinux.image_size, 0, vmlinux.bss_size); 1262e83e0ebSVasily Gorbik } 1272e83e0ebSVasily Gorbik 1288f75582aSVasily Gorbik void startup_kernel(void) 1298f75582aSVasily Gorbik { 130b2d24b97SGerald Schaefer unsigned long random_lma; 1319641b8ccSMartin Schwidefsky unsigned long safe_addr; 132369f91c3SVasily Gorbik void *img; 1338f75582aSVasily Gorbik 13449698745SVasily Gorbik store_ipl_parmblock(); 1359641b8ccSMartin Schwidefsky safe_addr = mem_safe_offset(); 1369641b8ccSMartin Schwidefsky safe_addr = read_ipl_report(safe_addr); 1379641b8ccSMartin Schwidefsky uv_query_info(); 1389641b8ccSMartin Schwidefsky rescue_initrd(safe_addr); 1399641b8ccSMartin Schwidefsky sclp_early_read_info(); 14049698745SVasily Gorbik setup_boot_command_line(); 141b5e80459SVasily Gorbik parse_boot_command_line(); 14249698745SVasily Gorbik setup_memory_end(); 1436966d604SVasily Gorbik detect_memory(); 144b2d24b97SGerald Schaefer 145b2d24b97SGerald Schaefer random_lma = __kaslr_offset = 0; 146b2d24b97SGerald Schaefer if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && kaslr_enabled) { 147b2d24b97SGerald Schaefer random_lma = get_random_base(safe_addr); 148b2d24b97SGerald Schaefer if (random_lma) { 149b2d24b97SGerald Schaefer __kaslr_offset = random_lma - vmlinux.default_lma; 150b2d24b97SGerald Schaefer img = (void *)vmlinux.default_lma; 151b2d24b97SGerald Schaefer vmlinux.default_lma += __kaslr_offset; 152b2d24b97SGerald Schaefer vmlinux.entry += __kaslr_offset; 153b2d24b97SGerald Schaefer vmlinux.bootdata_off += __kaslr_offset; 154b2d24b97SGerald Schaefer vmlinux.bootdata_preserved_off += __kaslr_offset; 155b2d24b97SGerald Schaefer vmlinux.rela_dyn_start += __kaslr_offset; 156b2d24b97SGerald Schaefer vmlinux.rela_dyn_end += __kaslr_offset; 157b2d24b97SGerald Schaefer vmlinux.dynsym_start += __kaslr_offset; 158b2d24b97SGerald Schaefer } 159b2d24b97SGerald Schaefer } 160b2d24b97SGerald Schaefer 1618f75582aSVasily Gorbik if (!IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED)) { 162369f91c3SVasily Gorbik img = decompress_kernel(); 163369f91c3SVasily Gorbik memmove((void *)vmlinux.default_lma, img, vmlinux.image_size); 164b2d24b97SGerald Schaefer } else if (__kaslr_offset) 165b2d24b97SGerald Schaefer memcpy((void *)vmlinux.default_lma, img, vmlinux.image_size); 166b2d24b97SGerald Schaefer 1672e83e0ebSVasily Gorbik clear_bss_section(); 168d1b52a43SVasily Gorbik copy_bootdata(); 169805bc0bcSGerald Schaefer if (IS_ENABLED(CONFIG_RELOCATABLE)) 170b2d24b97SGerald Schaefer handle_relocs(__kaslr_offset); 171b2d24b97SGerald Schaefer 172b2d24b97SGerald Schaefer if (__kaslr_offset) { 173a9f2f686SGerald Schaefer /* 174a9f2f686SGerald Schaefer * Save KASLR offset for early dumps, before vmcore_info is set. 175a9f2f686SGerald Schaefer * Mark as uneven to distinguish from real vmcore_info pointer. 176a9f2f686SGerald Schaefer */ 177a9f2f686SGerald Schaefer S390_lowcore.vmcore_info = __kaslr_offset | 0x1UL; 178b2d24b97SGerald Schaefer /* Clear non-relocated kernel */ 179b2d24b97SGerald Schaefer if (IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED)) 180b2d24b97SGerald Schaefer memset(img, 0, vmlinux.image_size); 181b2d24b97SGerald Schaefer } 182369f91c3SVasily Gorbik vmlinux.entry(); 1838f75582aSVasily Gorbik } 184