1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * S390 kdump implementation 4 * 5 * Copyright IBM Corp. 2011 6 * Author(s): Michael Holzheu <holzheu@linux.vnet.ibm.com> 7 */ 8 9 #include <linux/crash_dump.h> 10 #include <asm/lowcore.h> 11 #include <linux/kernel.h> 12 #include <linux/init.h> 13 #include <linux/mm.h> 14 #include <linux/gfp.h> 15 #include <linux/slab.h> 16 #include <linux/memblock.h> 17 #include <linux/elf.h> 18 #include <linux/uio.h> 19 #include <asm/asm-offsets.h> 20 #include <asm/os_info.h> 21 #include <asm/elf.h> 22 #include <asm/ipl.h> 23 #include <asm/sclp.h> 24 #include <asm/maccess.h> 25 #include <asm/fpu.h> 26 27 #define PTR_ADD(x, y) (((char *) (x)) + ((unsigned long) (y))) 28 #define PTR_SUB(x, y) (((char *) (x)) - ((unsigned long) (y))) 29 #define PTR_DIFF(x, y) ((unsigned long)(((char *) (x)) - ((unsigned long) (y)))) 30 31 static struct memblock_region oldmem_region; 32 33 static struct memblock_type oldmem_type = { 34 .cnt = 1, 35 .max = 1, 36 .total_size = 0, 37 .regions = &oldmem_region, 38 .name = "oldmem", 39 }; 40 41 struct save_area { 42 struct list_head list; 43 u64 psw[2]; 44 u64 ctrs[16]; 45 u64 gprs[16]; 46 u32 acrs[16]; 47 u64 fprs[16]; 48 u32 fpc; 49 u32 prefix; 50 u32 todpreg; 51 u64 timer; 52 u64 todcmp; 53 u64 vxrs_low[16]; 54 __vector128 vxrs_high[16]; 55 }; 56 57 static LIST_HEAD(dump_save_areas); 58 59 /* 60 * Allocate a save area 61 */ 62 struct save_area * __init save_area_alloc(bool is_boot_cpu) 63 { 64 struct save_area *sa; 65 66 sa = memblock_alloc_or_panic(sizeof(*sa), 8); 67 68 if (is_boot_cpu) 69 list_add(&sa->list, &dump_save_areas); 70 else 71 list_add_tail(&sa->list, &dump_save_areas); 72 return sa; 73 } 74 75 /* 76 * Return the address of the save area for the boot CPU 77 */ 78 struct save_area * __init save_area_boot_cpu(void) 79 { 80 return list_first_entry_or_null(&dump_save_areas, struct save_area, list); 81 } 82 83 /* 84 * Copy CPU registers into the save area 85 */ 86 void __init save_area_add_regs(struct save_area *sa, void *regs) 87 { 88 struct lowcore *lc; 89 90 lc = (struct lowcore *)(regs - __LC_FPREGS_SAVE_AREA); 91 memcpy(&sa->psw, &lc->psw_save_area, sizeof(sa->psw)); 92 memcpy(&sa->ctrs, &lc->cregs_save_area, sizeof(sa->ctrs)); 93 memcpy(&sa->gprs, &lc->gpregs_save_area, sizeof(sa->gprs)); 94 memcpy(&sa->acrs, &lc->access_regs_save_area, sizeof(sa->acrs)); 95 memcpy(&sa->fprs, &lc->floating_pt_save_area, sizeof(sa->fprs)); 96 memcpy(&sa->fpc, &lc->fpt_creg_save_area, sizeof(sa->fpc)); 97 memcpy(&sa->prefix, &lc->prefixreg_save_area, sizeof(sa->prefix)); 98 memcpy(&sa->todpreg, &lc->tod_progreg_save_area, sizeof(sa->todpreg)); 99 memcpy(&sa->timer, &lc->cpu_timer_save_area, sizeof(sa->timer)); 100 memcpy(&sa->todcmp, &lc->clock_comp_save_area, sizeof(sa->todcmp)); 101 } 102 103 /* 104 * Copy vector registers into the save area 105 */ 106 void __init save_area_add_vxrs(struct save_area *sa, __vector128 *vxrs) 107 { 108 int i; 109 110 /* Copy lower halves of vector registers 0-15 */ 111 for (i = 0; i < 16; i++) 112 sa->vxrs_low[i] = vxrs[i].low; 113 /* Copy vector registers 16-31 */ 114 memcpy(sa->vxrs_high, vxrs + 16, 16 * sizeof(__vector128)); 115 } 116 117 static size_t copy_oldmem_iter(struct iov_iter *iter, unsigned long src, size_t count) 118 { 119 size_t len, copied, res = 0; 120 121 while (count) { 122 if (!oldmem_data.start && src < sclp.hsa_size) { 123 /* Copy from zfcp/nvme dump HSA area */ 124 len = min(count, sclp.hsa_size - src); 125 copied = memcpy_hsa_iter(iter, src, len); 126 } else { 127 /* Check for swapped kdump oldmem areas */ 128 if (oldmem_data.start && src - oldmem_data.start < oldmem_data.size) { 129 src -= oldmem_data.start; 130 len = min(count, oldmem_data.size - src); 131 } else if (oldmem_data.start && src < oldmem_data.size) { 132 len = min(count, oldmem_data.size - src); 133 src += oldmem_data.start; 134 } else { 135 len = count; 136 } 137 copied = memcpy_real_iter(iter, src, len); 138 } 139 count -= copied; 140 src += copied; 141 res += copied; 142 if (copied < len) 143 break; 144 } 145 return res; 146 } 147 148 int copy_oldmem_kernel(void *dst, unsigned long src, size_t count) 149 { 150 struct iov_iter iter; 151 struct kvec kvec; 152 153 kvec.iov_base = dst; 154 kvec.iov_len = count; 155 iov_iter_kvec(&iter, ITER_DEST, &kvec, 1, count); 156 if (copy_oldmem_iter(&iter, src, count) < count) 157 return -EFAULT; 158 return 0; 159 } 160 161 /* 162 * Copy one page from "oldmem" 163 */ 164 ssize_t copy_oldmem_page(struct iov_iter *iter, unsigned long pfn, size_t csize, 165 unsigned long offset) 166 { 167 unsigned long src; 168 169 src = pfn_to_phys(pfn) + offset; 170 return copy_oldmem_iter(iter, src, csize); 171 } 172 173 /* 174 * Remap "oldmem" for kdump 175 * 176 * For the kdump reserved memory this functions performs a swap operation: 177 * [0 - OLDMEM_SIZE] is mapped to [OLDMEM_BASE - OLDMEM_BASE + OLDMEM_SIZE] 178 */ 179 static int remap_oldmem_pfn_range_kdump(struct vm_area_struct *vma, 180 unsigned long from, unsigned long pfn, 181 unsigned long size, pgprot_t prot) 182 { 183 unsigned long size_old; 184 int rc; 185 186 if (pfn < oldmem_data.size >> PAGE_SHIFT) { 187 size_old = min(size, oldmem_data.size - (pfn << PAGE_SHIFT)); 188 rc = remap_pfn_range(vma, from, 189 pfn + (oldmem_data.start >> PAGE_SHIFT), 190 size_old, prot); 191 if (rc || size == size_old) 192 return rc; 193 size -= size_old; 194 from += size_old; 195 pfn += size_old >> PAGE_SHIFT; 196 } 197 return remap_pfn_range(vma, from, pfn, size, prot); 198 } 199 200 /* 201 * Remap "oldmem" for zfcp/nvme dump 202 * 203 * We only map available memory above HSA size. Memory below HSA size 204 * is read on demand using the copy_oldmem_page() function. 205 */ 206 static int remap_oldmem_pfn_range_zfcpdump(struct vm_area_struct *vma, 207 unsigned long from, 208 unsigned long pfn, 209 unsigned long size, pgprot_t prot) 210 { 211 unsigned long hsa_end = sclp.hsa_size; 212 unsigned long size_hsa; 213 214 if (pfn < hsa_end >> PAGE_SHIFT) { 215 size_hsa = min(size, hsa_end - (pfn << PAGE_SHIFT)); 216 if (size == size_hsa) 217 return 0; 218 size -= size_hsa; 219 from += size_hsa; 220 pfn += size_hsa >> PAGE_SHIFT; 221 } 222 return remap_pfn_range(vma, from, pfn, size, prot); 223 } 224 225 /* 226 * Remap "oldmem" for kdump or zfcp/nvme dump 227 */ 228 int remap_oldmem_pfn_range(struct vm_area_struct *vma, unsigned long from, 229 unsigned long pfn, unsigned long size, pgprot_t prot) 230 { 231 if (oldmem_data.start) 232 return remap_oldmem_pfn_range_kdump(vma, from, pfn, size, prot); 233 else 234 return remap_oldmem_pfn_range_zfcpdump(vma, from, pfn, size, 235 prot); 236 } 237 238 /* 239 * Return true only when in a kdump or stand-alone kdump environment. 240 * Note that /proc/vmcore might also be available in "standard zfcp/nvme dump" 241 * environments, where this function returns false; see dump_available(). 242 */ 243 bool is_kdump_kernel(void) 244 { 245 return oldmem_data.start; 246 } 247 EXPORT_SYMBOL_GPL(is_kdump_kernel); 248 249 static const char *nt_name(Elf64_Word type) 250 { 251 const char *name = "LINUX"; 252 253 if (type == NT_PRPSINFO || type == NT_PRSTATUS || type == NT_PRFPREG) 254 name = KEXEC_CORE_NOTE_NAME; 255 return name; 256 } 257 258 /* 259 * Initialize ELF note 260 */ 261 static void *nt_init_name(void *buf, Elf64_Word type, void *desc, int d_len, 262 const char *name) 263 { 264 Elf64_Nhdr *note; 265 u64 len; 266 267 note = (Elf64_Nhdr *)buf; 268 note->n_namesz = strlen(name) + 1; 269 note->n_descsz = d_len; 270 note->n_type = type; 271 len = sizeof(Elf64_Nhdr); 272 273 memcpy(buf + len, name, note->n_namesz); 274 len = roundup(len + note->n_namesz, 4); 275 276 memcpy(buf + len, desc, note->n_descsz); 277 len = roundup(len + note->n_descsz, 4); 278 279 return PTR_ADD(buf, len); 280 } 281 282 static inline void *nt_init(void *buf, Elf64_Word type, void *desc, int d_len) 283 { 284 return nt_init_name(buf, type, desc, d_len, nt_name(type)); 285 } 286 287 /* 288 * Calculate the size of ELF note 289 */ 290 static size_t nt_size_name(int d_len, const char *name) 291 { 292 size_t size; 293 294 size = sizeof(Elf64_Nhdr); 295 size += roundup(strlen(name) + 1, 4); 296 size += roundup(d_len, 4); 297 298 return size; 299 } 300 301 static inline size_t nt_size(Elf64_Word type, int d_len) 302 { 303 return nt_size_name(d_len, nt_name(type)); 304 } 305 306 /* 307 * Fill ELF notes for one CPU with save area registers 308 */ 309 static void *fill_cpu_elf_notes(void *ptr, int cpu, struct save_area *sa) 310 { 311 struct elf_prstatus nt_prstatus; 312 elf_fpregset_t nt_fpregset; 313 314 /* Prepare prstatus note */ 315 memset(&nt_prstatus, 0, sizeof(nt_prstatus)); 316 memcpy(&nt_prstatus.pr_reg.gprs, sa->gprs, sizeof(sa->gprs)); 317 memcpy(&nt_prstatus.pr_reg.psw, sa->psw, sizeof(sa->psw)); 318 memcpy(&nt_prstatus.pr_reg.acrs, sa->acrs, sizeof(sa->acrs)); 319 nt_prstatus.common.pr_pid = cpu; 320 /* Prepare fpregset (floating point) note */ 321 memset(&nt_fpregset, 0, sizeof(nt_fpregset)); 322 memcpy(&nt_fpregset.fpc, &sa->fpc, sizeof(sa->fpc)); 323 memcpy(&nt_fpregset.fprs, &sa->fprs, sizeof(sa->fprs)); 324 /* Create ELF notes for the CPU */ 325 ptr = nt_init(ptr, NT_PRSTATUS, &nt_prstatus, sizeof(nt_prstatus)); 326 ptr = nt_init(ptr, NT_PRFPREG, &nt_fpregset, sizeof(nt_fpregset)); 327 ptr = nt_init(ptr, NT_S390_TIMER, &sa->timer, sizeof(sa->timer)); 328 ptr = nt_init(ptr, NT_S390_TODCMP, &sa->todcmp, sizeof(sa->todcmp)); 329 ptr = nt_init(ptr, NT_S390_TODPREG, &sa->todpreg, sizeof(sa->todpreg)); 330 ptr = nt_init(ptr, NT_S390_CTRS, &sa->ctrs, sizeof(sa->ctrs)); 331 ptr = nt_init(ptr, NT_S390_PREFIX, &sa->prefix, sizeof(sa->prefix)); 332 if (cpu_has_vx()) { 333 ptr = nt_init(ptr, NT_S390_VXRS_HIGH, 334 &sa->vxrs_high, sizeof(sa->vxrs_high)); 335 ptr = nt_init(ptr, NT_S390_VXRS_LOW, 336 &sa->vxrs_low, sizeof(sa->vxrs_low)); 337 } 338 return ptr; 339 } 340 341 /* 342 * Calculate size of ELF notes per cpu 343 */ 344 static size_t get_cpu_elf_notes_size(void) 345 { 346 struct save_area *sa = NULL; 347 size_t size; 348 349 size = nt_size(NT_PRSTATUS, sizeof(struct elf_prstatus)); 350 size += nt_size(NT_PRFPREG, sizeof(elf_fpregset_t)); 351 size += nt_size(NT_S390_TIMER, sizeof(sa->timer)); 352 size += nt_size(NT_S390_TODCMP, sizeof(sa->todcmp)); 353 size += nt_size(NT_S390_TODPREG, sizeof(sa->todpreg)); 354 size += nt_size(NT_S390_CTRS, sizeof(sa->ctrs)); 355 size += nt_size(NT_S390_PREFIX, sizeof(sa->prefix)); 356 if (cpu_has_vx()) { 357 size += nt_size(NT_S390_VXRS_HIGH, sizeof(sa->vxrs_high)); 358 size += nt_size(NT_S390_VXRS_LOW, sizeof(sa->vxrs_low)); 359 } 360 361 return size; 362 } 363 364 /* 365 * Initialize prpsinfo note (new kernel) 366 */ 367 static void *nt_prpsinfo(void *ptr) 368 { 369 struct elf_prpsinfo prpsinfo; 370 371 memset(&prpsinfo, 0, sizeof(prpsinfo)); 372 prpsinfo.pr_sname = 'R'; 373 strcpy(prpsinfo.pr_fname, "vmlinux"); 374 return nt_init(ptr, NT_PRPSINFO, &prpsinfo, sizeof(prpsinfo)); 375 } 376 377 /* 378 * Get vmcoreinfo using lowcore->vmcore_info (new kernel) 379 */ 380 static void *get_vmcoreinfo_old(unsigned long *size) 381 { 382 char nt_name[11], *vmcoreinfo; 383 unsigned long addr; 384 Elf64_Nhdr note; 385 386 if (copy_oldmem_kernel(&addr, __LC_VMCORE_INFO, sizeof(addr))) 387 return NULL; 388 memset(nt_name, 0, sizeof(nt_name)); 389 if (copy_oldmem_kernel(¬e, addr, sizeof(note))) 390 return NULL; 391 if (copy_oldmem_kernel(nt_name, addr + sizeof(note), 392 sizeof(nt_name) - 1)) 393 return NULL; 394 if (strcmp(nt_name, VMCOREINFO_NOTE_NAME) != 0) 395 return NULL; 396 vmcoreinfo = kzalloc(note.n_descsz, GFP_KERNEL); 397 if (!vmcoreinfo) 398 return NULL; 399 if (copy_oldmem_kernel(vmcoreinfo, addr + 24, note.n_descsz)) { 400 kfree(vmcoreinfo); 401 return NULL; 402 } 403 *size = note.n_descsz; 404 return vmcoreinfo; 405 } 406 407 /* 408 * Initialize vmcoreinfo note (new kernel) 409 */ 410 static void *nt_vmcoreinfo(void *ptr) 411 { 412 const char *name = VMCOREINFO_NOTE_NAME; 413 unsigned long size; 414 void *vmcoreinfo; 415 416 vmcoreinfo = os_info_old_entry(OS_INFO_VMCOREINFO, &size); 417 if (vmcoreinfo) 418 return nt_init_name(ptr, 0, vmcoreinfo, size, name); 419 420 vmcoreinfo = get_vmcoreinfo_old(&size); 421 if (!vmcoreinfo) 422 return ptr; 423 ptr = nt_init_name(ptr, 0, vmcoreinfo, size, name); 424 kfree(vmcoreinfo); 425 return ptr; 426 } 427 428 static size_t nt_vmcoreinfo_size(void) 429 { 430 const char *name = VMCOREINFO_NOTE_NAME; 431 unsigned long size; 432 void *vmcoreinfo; 433 434 vmcoreinfo = os_info_old_entry(OS_INFO_VMCOREINFO, &size); 435 if (vmcoreinfo) 436 return nt_size_name(size, name); 437 438 vmcoreinfo = get_vmcoreinfo_old(&size); 439 if (!vmcoreinfo) 440 return 0; 441 442 kfree(vmcoreinfo); 443 return nt_size_name(size, name); 444 } 445 446 /* 447 * Initialize final note (needed for /proc/vmcore code) 448 */ 449 static void *nt_final(void *ptr) 450 { 451 Elf64_Nhdr *note; 452 453 note = (Elf64_Nhdr *) ptr; 454 note->n_namesz = 0; 455 note->n_descsz = 0; 456 note->n_type = 0; 457 return PTR_ADD(ptr, sizeof(Elf64_Nhdr)); 458 } 459 460 /* 461 * Initialize ELF header (new kernel) 462 */ 463 static void *ehdr_init(Elf64_Ehdr *ehdr, int phdr_count) 464 { 465 memset(ehdr, 0, sizeof(*ehdr)); 466 memcpy(ehdr->e_ident, ELFMAG, SELFMAG); 467 ehdr->e_ident[EI_CLASS] = ELFCLASS64; 468 ehdr->e_ident[EI_DATA] = ELFDATA2MSB; 469 ehdr->e_ident[EI_VERSION] = EV_CURRENT; 470 memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD); 471 ehdr->e_type = ET_CORE; 472 ehdr->e_machine = EM_S390; 473 ehdr->e_version = EV_CURRENT; 474 ehdr->e_phoff = sizeof(Elf64_Ehdr); 475 ehdr->e_ehsize = sizeof(Elf64_Ehdr); 476 ehdr->e_phentsize = sizeof(Elf64_Phdr); 477 /* Number of PT_LOAD program headers plus PT_NOTE program header */ 478 ehdr->e_phnum = phdr_count + 1; 479 return ehdr + 1; 480 } 481 482 /* 483 * Return CPU count for ELF header (new kernel) 484 */ 485 static int get_cpu_cnt(void) 486 { 487 struct save_area *sa; 488 int cpus = 0; 489 490 list_for_each_entry(sa, &dump_save_areas, list) 491 if (sa->prefix != 0) 492 cpus++; 493 return cpus; 494 } 495 496 /* 497 * Return memory chunk count for ELF header (new kernel) 498 */ 499 static int get_mem_chunk_cnt(void) 500 { 501 int cnt = 0; 502 u64 idx; 503 504 for_each_physmem_range(idx, &oldmem_type, NULL, NULL) 505 cnt++; 506 return cnt; 507 } 508 509 static void fill_ptload(Elf64_Phdr *phdr, unsigned long paddr, 510 unsigned long vaddr, unsigned long size) 511 { 512 phdr->p_type = PT_LOAD; 513 phdr->p_vaddr = vaddr; 514 phdr->p_offset = paddr; 515 phdr->p_paddr = paddr; 516 phdr->p_filesz = size; 517 phdr->p_memsz = size; 518 phdr->p_flags = PF_R | PF_W | PF_X; 519 phdr->p_align = PAGE_SIZE; 520 } 521 522 /* 523 * Initialize ELF loads (new kernel) 524 */ 525 static void loads_init(Elf64_Phdr *phdr, bool os_info_has_vm) 526 { 527 unsigned long old_identity_base = 0; 528 phys_addr_t start, end; 529 u64 idx; 530 531 if (os_info_has_vm) 532 old_identity_base = os_info_old_value(OS_INFO_IDENTITY_BASE); 533 for_each_physmem_range(idx, &oldmem_type, &start, &end) { 534 fill_ptload(phdr, start, old_identity_base + start, 535 end - start); 536 phdr++; 537 } 538 } 539 540 static bool os_info_has_vm(void) 541 { 542 return os_info_old_value(OS_INFO_KASLR_OFFSET); 543 } 544 545 #ifdef CONFIG_PROC_VMCORE_DEVICE_RAM 546 /* 547 * Fill PT_LOAD for a physical memory range owned by a device and detected by 548 * its device driver. 549 */ 550 void elfcorehdr_fill_device_ram_ptload_elf64(Elf64_Phdr *phdr, 551 unsigned long long paddr, unsigned long long size) 552 { 553 unsigned long old_identity_base = 0; 554 555 if (os_info_has_vm()) 556 old_identity_base = os_info_old_value(OS_INFO_IDENTITY_BASE); 557 fill_ptload(phdr, paddr, old_identity_base + paddr, size); 558 } 559 #endif 560 561 /* 562 * Prepare PT_LOAD type program header for kernel image region 563 */ 564 static void text_init(Elf64_Phdr *phdr) 565 { 566 unsigned long start_phys = os_info_old_value(OS_INFO_IMAGE_PHYS); 567 unsigned long start = os_info_old_value(OS_INFO_IMAGE_START); 568 unsigned long end = os_info_old_value(OS_INFO_IMAGE_END); 569 570 phdr->p_type = PT_LOAD; 571 phdr->p_vaddr = start; 572 phdr->p_filesz = end - start; 573 phdr->p_memsz = end - start; 574 phdr->p_offset = start_phys; 575 phdr->p_paddr = start_phys; 576 phdr->p_flags = PF_R | PF_W | PF_X; 577 phdr->p_align = PAGE_SIZE; 578 } 579 580 /* 581 * Initialize notes (new kernel) 582 */ 583 static void *notes_init(Elf64_Phdr *phdr, void *ptr, u64 notes_offset) 584 { 585 struct save_area *sa; 586 void *ptr_start = ptr; 587 int cpu; 588 589 ptr = nt_prpsinfo(ptr); 590 591 cpu = 1; 592 list_for_each_entry(sa, &dump_save_areas, list) 593 if (sa->prefix != 0) 594 ptr = fill_cpu_elf_notes(ptr, cpu++, sa); 595 ptr = nt_vmcoreinfo(ptr); 596 ptr = nt_final(ptr); 597 memset(phdr, 0, sizeof(*phdr)); 598 phdr->p_type = PT_NOTE; 599 phdr->p_offset = notes_offset; 600 phdr->p_filesz = (unsigned long) PTR_SUB(ptr, ptr_start); 601 phdr->p_memsz = phdr->p_filesz; 602 return ptr; 603 } 604 605 static size_t get_elfcorehdr_size(int phdr_count) 606 { 607 size_t size; 608 609 size = sizeof(Elf64_Ehdr); 610 /* PT_NOTES */ 611 size += sizeof(Elf64_Phdr); 612 /* nt_prpsinfo */ 613 size += nt_size(NT_PRPSINFO, sizeof(struct elf_prpsinfo)); 614 /* regsets */ 615 size += get_cpu_cnt() * get_cpu_elf_notes_size(); 616 /* nt_vmcoreinfo */ 617 size += nt_vmcoreinfo_size(); 618 /* nt_final */ 619 size += sizeof(Elf64_Nhdr); 620 /* PT_LOADS */ 621 size += phdr_count * sizeof(Elf64_Phdr); 622 623 return size; 624 } 625 626 /* 627 * Create ELF core header (new kernel) 628 */ 629 int elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size) 630 { 631 Elf64_Phdr *phdr_notes, *phdr_loads, *phdr_text; 632 int mem_chunk_cnt, phdr_text_cnt; 633 size_t alloc_size; 634 void *ptr, *hdr; 635 u64 hdr_off; 636 637 /* If we are not in kdump or zfcp/nvme dump mode return */ 638 if (!oldmem_data.start && !is_ipl_type_dump()) 639 return 0; 640 /* If we cannot get HSA size for zfcp/nvme dump return error */ 641 if (is_ipl_type_dump() && !sclp.hsa_size) 642 return -ENODEV; 643 644 /* For kdump, exclude previous crashkernel memory */ 645 if (oldmem_data.start) { 646 oldmem_region.base = oldmem_data.start; 647 oldmem_region.size = oldmem_data.size; 648 oldmem_type.total_size = oldmem_data.size; 649 } 650 651 mem_chunk_cnt = get_mem_chunk_cnt(); 652 phdr_text_cnt = os_info_has_vm() ? 1 : 0; 653 654 alloc_size = get_elfcorehdr_size(mem_chunk_cnt + phdr_text_cnt); 655 656 hdr = kzalloc(alloc_size, GFP_KERNEL); 657 658 /* 659 * Without elfcorehdr /proc/vmcore cannot be created. Thus creating 660 * a dump with this crash kernel will fail. Panic now to allow other 661 * dump mechanisms to take over. 662 */ 663 if (!hdr) 664 panic("s390 kdump allocating elfcorehdr failed"); 665 666 /* Init elf header */ 667 phdr_notes = ehdr_init(hdr, mem_chunk_cnt + phdr_text_cnt); 668 /* Init program headers */ 669 if (phdr_text_cnt) { 670 phdr_text = phdr_notes + 1; 671 phdr_loads = phdr_text + 1; 672 } else { 673 phdr_loads = phdr_notes + 1; 674 } 675 ptr = PTR_ADD(phdr_loads, sizeof(Elf64_Phdr) * mem_chunk_cnt); 676 /* Init notes */ 677 hdr_off = PTR_DIFF(ptr, hdr); 678 ptr = notes_init(phdr_notes, ptr, ((unsigned long) hdr) + hdr_off); 679 /* Init kernel text program header */ 680 if (phdr_text_cnt) 681 text_init(phdr_text); 682 /* Init loads */ 683 loads_init(phdr_loads, phdr_text_cnt); 684 /* Finalize program headers */ 685 hdr_off = PTR_DIFF(ptr, hdr); 686 *addr = (unsigned long long) hdr; 687 *size = (unsigned long long) hdr_off; 688 BUG_ON(elfcorehdr_size > alloc_size); 689 return 0; 690 } 691 692 /* 693 * Free ELF core header (new kernel) 694 */ 695 void elfcorehdr_free(unsigned long long addr) 696 { 697 kfree((void *)(unsigned long)addr); 698 } 699 700 /* 701 * Read from ELF header 702 */ 703 ssize_t elfcorehdr_read(char *buf, size_t count, u64 *ppos) 704 { 705 void *src = (void *)(unsigned long)*ppos; 706 707 memcpy(buf, src, count); 708 *ppos += count; 709 return count; 710 } 711 712 /* 713 * Read from ELF notes data 714 */ 715 ssize_t elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos) 716 { 717 void *src = (void *)(unsigned long)*ppos; 718 719 memcpy(buf, src, count); 720 *ppos += count; 721 return count; 722 } 723