1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * s390 code for kexec_file_load system call 4 * 5 * Copyright IBM Corp. 2018 6 * 7 * Author(s): Philipp Rudo <prudo@linux.vnet.ibm.com> 8 */ 9 10 #include <linux/elf.h> 11 #include <linux/kexec.h> 12 #include <asm/setup.h> 13 14 const struct kexec_file_ops * const kexec_file_loaders[] = { 15 &s390_kexec_elf_ops, 16 &s390_kexec_image_ops, 17 NULL, 18 }; 19 20 int *kexec_file_update_kernel(struct kimage *image, 21 struct s390_load_data *data) 22 { 23 unsigned long *loc; 24 25 if (image->cmdline_buf_len >= ARCH_COMMAND_LINE_SIZE) 26 return ERR_PTR(-EINVAL); 27 28 if (image->cmdline_buf_len) 29 memcpy(data->kernel_buf + COMMAND_LINE_OFFSET, 30 image->cmdline_buf, image->cmdline_buf_len); 31 32 if (image->type == KEXEC_TYPE_CRASH) { 33 loc = (unsigned long *)(data->kernel_buf + OLDMEM_BASE_OFFSET); 34 *loc = crashk_res.start; 35 36 loc = (unsigned long *)(data->kernel_buf + OLDMEM_SIZE_OFFSET); 37 *loc = crashk_res.end - crashk_res.start + 1; 38 } 39 40 if (image->initrd_buf) { 41 loc = (unsigned long *)(data->kernel_buf + INITRD_START_OFFSET); 42 *loc = data->initrd_load_addr; 43 44 loc = (unsigned long *)(data->kernel_buf + INITRD_SIZE_OFFSET); 45 *loc = image->initrd_buf_len; 46 } 47 48 return NULL; 49 } 50 51 static int kexec_file_update_purgatory(struct kimage *image) 52 { 53 u64 entry, type; 54 int ret; 55 56 if (image->type == KEXEC_TYPE_CRASH) { 57 entry = STARTUP_KDUMP_OFFSET; 58 type = KEXEC_TYPE_CRASH; 59 } else { 60 entry = STARTUP_NORMAL_OFFSET; 61 type = KEXEC_TYPE_DEFAULT; 62 } 63 64 ret = kexec_purgatory_get_set_symbol(image, "kernel_entry", &entry, 65 sizeof(entry), false); 66 if (ret) 67 return ret; 68 69 ret = kexec_purgatory_get_set_symbol(image, "kernel_type", &type, 70 sizeof(type), false); 71 if (ret) 72 return ret; 73 74 if (image->type == KEXEC_TYPE_CRASH) { 75 u64 crash_size; 76 77 ret = kexec_purgatory_get_set_symbol(image, "crash_start", 78 &crashk_res.start, 79 sizeof(crashk_res.start), 80 false); 81 if (ret) 82 return ret; 83 84 crash_size = crashk_res.end - crashk_res.start + 1; 85 ret = kexec_purgatory_get_set_symbol(image, "crash_size", 86 &crash_size, 87 sizeof(crash_size), 88 false); 89 } 90 return ret; 91 } 92 93 int kexec_file_add_purgatory(struct kimage *image, struct s390_load_data *data) 94 { 95 struct kexec_buf buf; 96 int ret; 97 98 buf.image = image; 99 100 data->memsz = ALIGN(data->memsz, PAGE_SIZE); 101 buf.mem = data->memsz; 102 if (image->type == KEXEC_TYPE_CRASH) 103 buf.mem += crashk_res.start; 104 105 ret = kexec_load_purgatory(image, &buf); 106 if (ret) 107 return ret; 108 109 ret = kexec_file_update_purgatory(image); 110 return ret; 111 } 112 113 int kexec_file_add_initrd(struct kimage *image, struct s390_load_data *data, 114 char *initrd, unsigned long initrd_len) 115 { 116 struct kexec_buf buf; 117 int ret; 118 119 buf.image = image; 120 121 buf.buffer = initrd; 122 buf.bufsz = initrd_len; 123 124 data->memsz = ALIGN(data->memsz, PAGE_SIZE); 125 buf.mem = data->memsz; 126 if (image->type == KEXEC_TYPE_CRASH) 127 buf.mem += crashk_res.start; 128 buf.memsz = buf.bufsz; 129 130 data->initrd_load_addr = buf.mem; 131 data->memsz += buf.memsz; 132 133 ret = kexec_add_buffer(&buf); 134 return ret; 135 } 136 137 /* 138 * The kernel is loaded to a fixed location. Turn off kexec_locate_mem_hole 139 * and provide kbuf->mem by hand. 140 */ 141 int arch_kexec_walk_mem(struct kexec_buf *kbuf, 142 int (*func)(struct resource *, void *)) 143 { 144 return 1; 145 } 146 147 int arch_kexec_apply_relocations_add(struct purgatory_info *pi, 148 Elf_Shdr *section, 149 const Elf_Shdr *relsec, 150 const Elf_Shdr *symtab) 151 { 152 Elf_Rela *relas; 153 int i; 154 155 relas = (void *)pi->ehdr + relsec->sh_offset; 156 157 for (i = 0; i < relsec->sh_size / sizeof(*relas); i++) { 158 const Elf_Sym *sym; /* symbol to relocate */ 159 unsigned long addr; /* final location after relocation */ 160 unsigned long val; /* relocated symbol value */ 161 void *loc; /* tmp location to modify */ 162 163 sym = (void *)pi->ehdr + symtab->sh_offset; 164 sym += ELF64_R_SYM(relas[i].r_info); 165 166 if (sym->st_shndx == SHN_UNDEF) 167 return -ENOEXEC; 168 169 if (sym->st_shndx == SHN_COMMON) 170 return -ENOEXEC; 171 172 if (sym->st_shndx >= pi->ehdr->e_shnum && 173 sym->st_shndx != SHN_ABS) 174 return -ENOEXEC; 175 176 loc = pi->purgatory_buf; 177 loc += section->sh_offset; 178 loc += relas[i].r_offset; 179 180 val = sym->st_value; 181 if (sym->st_shndx != SHN_ABS) 182 val += pi->sechdrs[sym->st_shndx].sh_addr; 183 val += relas[i].r_addend; 184 185 addr = section->sh_addr + relas[i].r_offset; 186 187 switch (ELF64_R_TYPE(relas[i].r_info)) { 188 case R_390_8: /* Direct 8 bit. */ 189 *(u8 *)loc = val; 190 break; 191 case R_390_12: /* Direct 12 bit. */ 192 *(u16 *)loc &= 0xf000; 193 *(u16 *)loc |= val & 0xfff; 194 break; 195 case R_390_16: /* Direct 16 bit. */ 196 *(u16 *)loc = val; 197 break; 198 case R_390_20: /* Direct 20 bit. */ 199 *(u32 *)loc &= 0xf00000ff; 200 *(u32 *)loc |= (val & 0xfff) << 16; /* DL */ 201 *(u32 *)loc |= (val & 0xff000) >> 4; /* DH */ 202 break; 203 case R_390_32: /* Direct 32 bit. */ 204 *(u32 *)loc = val; 205 break; 206 case R_390_64: /* Direct 64 bit. */ 207 *(u64 *)loc = val; 208 break; 209 case R_390_PC16: /* PC relative 16 bit. */ 210 *(u16 *)loc = (val - addr); 211 break; 212 case R_390_PC16DBL: /* PC relative 16 bit shifted by 1. */ 213 *(u16 *)loc = (val - addr) >> 1; 214 break; 215 case R_390_PC32DBL: /* PC relative 32 bit shifted by 1. */ 216 *(u32 *)loc = (val - addr) >> 1; 217 break; 218 case R_390_PC32: /* PC relative 32 bit. */ 219 *(u32 *)loc = (val - addr); 220 break; 221 case R_390_PC64: /* PC relative 64 bit. */ 222 *(u64 *)loc = (val - addr); 223 break; 224 default: 225 break; 226 } 227 } 228 return 0; 229 } 230 231 int arch_kexec_kernel_image_probe(struct kimage *image, void *buf, 232 unsigned long buf_len) 233 { 234 /* A kernel must be at least large enough to contain head.S. During 235 * load memory in head.S will be accessed, e.g. to register the next 236 * command line. If the next kernel were smaller the current kernel 237 * will panic at load. 238 * 239 * 0x11000 = sizeof(head.S) 240 */ 241 if (buf_len < 0x11000) 242 return -ENOEXEC; 243 244 return kexec_image_probe_default(image, buf, buf_len); 245 } 246