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