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 }; 49a80313ffSGerald Schaefer 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 607516fc11SVasily Gorbik disabled_wait(0xdeadbeef); 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; 104805bc0bcSGerald Schaefer val = rela->r_addend + offset; 105805bc0bcSGerald Schaefer r_sym = ELF64_R_SYM(rela->r_info); 106805bc0bcSGerald Schaefer if (r_sym) 107805bc0bcSGerald Schaefer val += dynsym[r_sym].st_value; 108805bc0bcSGerald Schaefer r_type = ELF64_R_TYPE(rela->r_info); 109805bc0bcSGerald Schaefer rc = arch_kexec_do_relocs(r_type, (void *) loc, val, 0); 110805bc0bcSGerald Schaefer if (rc) 111805bc0bcSGerald Schaefer error("Unknown relocation type"); 112805bc0bcSGerald Schaefer } 113805bc0bcSGerald Schaefer } 114805bc0bcSGerald Schaefer 1158f75582aSVasily Gorbik void startup_kernel(void) 1168f75582aSVasily Gorbik { 117b2d24b97SGerald Schaefer unsigned long random_lma; 1189641b8ccSMartin Schwidefsky unsigned long safe_addr; 119369f91c3SVasily Gorbik void *img; 1208f75582aSVasily Gorbik 12149698745SVasily Gorbik store_ipl_parmblock(); 1229641b8ccSMartin Schwidefsky safe_addr = mem_safe_offset(); 1239641b8ccSMartin Schwidefsky safe_addr = read_ipl_report(safe_addr); 1249641b8ccSMartin Schwidefsky uv_query_info(); 1259641b8ccSMartin Schwidefsky rescue_initrd(safe_addr); 1269641b8ccSMartin Schwidefsky sclp_early_read_info(); 12749698745SVasily Gorbik setup_boot_command_line(); 128b5e80459SVasily Gorbik parse_boot_command_line(); 12949698745SVasily Gorbik setup_memory_end(); 1306966d604SVasily Gorbik detect_memory(); 131b2d24b97SGerald Schaefer 132b2d24b97SGerald Schaefer random_lma = __kaslr_offset = 0; 133b2d24b97SGerald Schaefer if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && kaslr_enabled) { 134b2d24b97SGerald Schaefer random_lma = get_random_base(safe_addr); 135b2d24b97SGerald Schaefer if (random_lma) { 136b2d24b97SGerald Schaefer __kaslr_offset = random_lma - vmlinux.default_lma; 137b2d24b97SGerald Schaefer img = (void *)vmlinux.default_lma; 138b2d24b97SGerald Schaefer vmlinux.default_lma += __kaslr_offset; 139b2d24b97SGerald Schaefer vmlinux.entry += __kaslr_offset; 140b2d24b97SGerald Schaefer vmlinux.bootdata_off += __kaslr_offset; 141b2d24b97SGerald Schaefer vmlinux.bootdata_preserved_off += __kaslr_offset; 142b2d24b97SGerald Schaefer vmlinux.rela_dyn_start += __kaslr_offset; 143b2d24b97SGerald Schaefer vmlinux.rela_dyn_end += __kaslr_offset; 144b2d24b97SGerald Schaefer vmlinux.dynsym_start += __kaslr_offset; 145b2d24b97SGerald Schaefer } 146b2d24b97SGerald Schaefer } 147b2d24b97SGerald Schaefer 1488f75582aSVasily Gorbik if (!IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED)) { 149369f91c3SVasily Gorbik img = decompress_kernel(); 150369f91c3SVasily Gorbik memmove((void *)vmlinux.default_lma, img, vmlinux.image_size); 151b2d24b97SGerald Schaefer } else if (__kaslr_offset) 152b2d24b97SGerald Schaefer memcpy((void *)vmlinux.default_lma, img, vmlinux.image_size); 153b2d24b97SGerald Schaefer 154d1b52a43SVasily Gorbik copy_bootdata(); 155805bc0bcSGerald Schaefer if (IS_ENABLED(CONFIG_RELOCATABLE)) 156b2d24b97SGerald Schaefer handle_relocs(__kaslr_offset); 157b2d24b97SGerald Schaefer 158b2d24b97SGerald Schaefer if (__kaslr_offset) { 159b2d24b97SGerald Schaefer /* Clear non-relocated kernel */ 160b2d24b97SGerald Schaefer if (IS_ENABLED(CONFIG_KERNEL_UNCOMPRESSED)) 161b2d24b97SGerald Schaefer memset(img, 0, vmlinux.image_size); 162b2d24b97SGerald Schaefer } 163369f91c3SVasily Gorbik vmlinux.entry(); 1648f75582aSVasily Gorbik } 165