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