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