xref: /linux/arch/s390/kernel/crash_dump.c (revision ec63e2a4897075e427c121d863bd89c44578094f)
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 (!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, void *src, size_t count)
136 {
137 	unsigned long from, len;
138 	void *ra;
139 	int rc;
140 
141 	while (count) {
142 		from = __pa(src);
143 		if (!OLDMEM_BASE && from < sclp.hsa_size) {
144 			/* Copy from zfcpdump HSA area */
145 			len = min(count, sclp.hsa_size - from);
146 			rc = memcpy_hsa_kernel(dst, from, len);
147 			if (rc)
148 				return rc;
149 		} else {
150 			/* Check for swapped kdump oldmem areas */
151 			if (OLDMEM_BASE && from - OLDMEM_BASE < OLDMEM_SIZE) {
152 				from -= OLDMEM_BASE;
153 				len = min(count, OLDMEM_SIZE - from);
154 			} else if (OLDMEM_BASE && from < OLDMEM_SIZE) {
155 				len = min(count, OLDMEM_SIZE - from);
156 				from += OLDMEM_BASE;
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, (void *) from, 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, void *src, size_t count)
180 {
181 	unsigned long from, len;
182 	int rc;
183 
184 	while (count) {
185 		from = __pa(src);
186 		if (!OLDMEM_BASE && from < sclp.hsa_size) {
187 			/* Copy from zfcpdump HSA area */
188 			len = min(count, sclp.hsa_size - from);
189 			rc = memcpy_hsa_user(dst, from, len);
190 			if (rc)
191 				return rc;
192 		} else {
193 			/* Check for swapped kdump oldmem areas */
194 			if (OLDMEM_BASE && from - OLDMEM_BASE < OLDMEM_SIZE) {
195 				from -= OLDMEM_BASE;
196 				len = min(count, OLDMEM_SIZE - from);
197 			} else if (OLDMEM_BASE && from < OLDMEM_SIZE) {
198 				len = min(count, OLDMEM_SIZE - from);
199 				from += OLDMEM_BASE;
200 			} else {
201 				len = count;
202 			}
203 			rc = copy_to_user_real(dst, (void *) from, count);
204 			if (rc)
205 				return rc;
206 		}
207 		dst += len;
208 		src += len;
209 		count -= len;
210 	}
211 	return 0;
212 }
213 
214 /*
215  * Copy one page from "oldmem"
216  */
217 ssize_t copy_oldmem_page(unsigned long pfn, char *buf, size_t csize,
218 			 unsigned long offset, int userbuf)
219 {
220 	void *src;
221 	int rc;
222 
223 	if (!csize)
224 		return 0;
225 	src = (void *) (pfn << PAGE_SHIFT) + offset;
226 	if (userbuf)
227 		rc = copy_oldmem_user((void __force __user *) buf, src, csize);
228 	else
229 		rc = copy_oldmem_kernel((void *) buf, src, csize);
230 	return rc;
231 }
232 
233 /*
234  * Remap "oldmem" for kdump
235  *
236  * For the kdump reserved memory this functions performs a swap operation:
237  * [0 - OLDMEM_SIZE] is mapped to [OLDMEM_BASE - OLDMEM_BASE + OLDMEM_SIZE]
238  */
239 static int remap_oldmem_pfn_range_kdump(struct vm_area_struct *vma,
240 					unsigned long from, unsigned long pfn,
241 					unsigned long size, pgprot_t prot)
242 {
243 	unsigned long size_old;
244 	int rc;
245 
246 	if (pfn < OLDMEM_SIZE >> PAGE_SHIFT) {
247 		size_old = min(size, OLDMEM_SIZE - (pfn << PAGE_SHIFT));
248 		rc = remap_pfn_range(vma, from,
249 				     pfn + (OLDMEM_BASE >> PAGE_SHIFT),
250 				     size_old, prot);
251 		if (rc || size == size_old)
252 			return rc;
253 		size -= size_old;
254 		from += size_old;
255 		pfn += size_old >> PAGE_SHIFT;
256 	}
257 	return remap_pfn_range(vma, from, pfn, size, prot);
258 }
259 
260 /*
261  * Remap "oldmem" for zfcpdump
262  *
263  * We only map available memory above HSA size. Memory below HSA size
264  * is read on demand using the copy_oldmem_page() function.
265  */
266 static int remap_oldmem_pfn_range_zfcpdump(struct vm_area_struct *vma,
267 					   unsigned long from,
268 					   unsigned long pfn,
269 					   unsigned long size, pgprot_t prot)
270 {
271 	unsigned long hsa_end = sclp.hsa_size;
272 	unsigned long size_hsa;
273 
274 	if (pfn < hsa_end >> PAGE_SHIFT) {
275 		size_hsa = min(size, hsa_end - (pfn << PAGE_SHIFT));
276 		if (size == size_hsa)
277 			return 0;
278 		size -= size_hsa;
279 		from += size_hsa;
280 		pfn += size_hsa >> PAGE_SHIFT;
281 	}
282 	return remap_pfn_range(vma, from, pfn, size, prot);
283 }
284 
285 /*
286  * Remap "oldmem" for kdump or zfcpdump
287  */
288 int remap_oldmem_pfn_range(struct vm_area_struct *vma, unsigned long from,
289 			   unsigned long pfn, unsigned long size, pgprot_t prot)
290 {
291 	if (OLDMEM_BASE)
292 		return remap_oldmem_pfn_range_kdump(vma, from, pfn, size, prot);
293 	else
294 		return remap_oldmem_pfn_range_zfcpdump(vma, from, pfn, size,
295 						       prot);
296 }
297 
298 static const char *nt_name(Elf64_Word type)
299 {
300 	const char *name = "LINUX";
301 
302 	if (type == NT_PRPSINFO || type == NT_PRSTATUS || type == NT_PRFPREG)
303 		name = KEXEC_CORE_NOTE_NAME;
304 	return name;
305 }
306 
307 /*
308  * Initialize ELF note
309  */
310 static void *nt_init_name(void *buf, Elf64_Word type, void *desc, int d_len,
311 			  const char *name)
312 {
313 	Elf64_Nhdr *note;
314 	u64 len;
315 
316 	note = (Elf64_Nhdr *)buf;
317 	note->n_namesz = strlen(name) + 1;
318 	note->n_descsz = d_len;
319 	note->n_type = type;
320 	len = sizeof(Elf64_Nhdr);
321 
322 	memcpy(buf + len, name, note->n_namesz);
323 	len = roundup(len + note->n_namesz, 4);
324 
325 	memcpy(buf + len, desc, note->n_descsz);
326 	len = roundup(len + note->n_descsz, 4);
327 
328 	return PTR_ADD(buf, len);
329 }
330 
331 static inline void *nt_init(void *buf, Elf64_Word type, void *desc, int d_len)
332 {
333 	return nt_init_name(buf, type, desc, d_len, nt_name(type));
334 }
335 
336 /*
337  * Calculate the size of ELF note
338  */
339 static size_t nt_size_name(int d_len, const char *name)
340 {
341 	size_t size;
342 
343 	size = sizeof(Elf64_Nhdr);
344 	size += roundup(strlen(name) + 1, 4);
345 	size += roundup(d_len, 4);
346 
347 	return size;
348 }
349 
350 static inline size_t nt_size(Elf64_Word type, int d_len)
351 {
352 	return nt_size_name(d_len, nt_name(type));
353 }
354 
355 /*
356  * Fill ELF notes for one CPU with save area registers
357  */
358 static void *fill_cpu_elf_notes(void *ptr, int cpu, struct save_area *sa)
359 {
360 	struct elf_prstatus nt_prstatus;
361 	elf_fpregset_t nt_fpregset;
362 
363 	/* Prepare prstatus note */
364 	memset(&nt_prstatus, 0, sizeof(nt_prstatus));
365 	memcpy(&nt_prstatus.pr_reg.gprs, sa->gprs, sizeof(sa->gprs));
366 	memcpy(&nt_prstatus.pr_reg.psw, sa->psw, sizeof(sa->psw));
367 	memcpy(&nt_prstatus.pr_reg.acrs, sa->acrs, sizeof(sa->acrs));
368 	nt_prstatus.pr_pid = cpu;
369 	/* Prepare fpregset (floating point) note */
370 	memset(&nt_fpregset, 0, sizeof(nt_fpregset));
371 	memcpy(&nt_fpregset.fpc, &sa->fpc, sizeof(sa->fpc));
372 	memcpy(&nt_fpregset.fprs, &sa->fprs, sizeof(sa->fprs));
373 	/* Create ELF notes for the CPU */
374 	ptr = nt_init(ptr, NT_PRSTATUS, &nt_prstatus, sizeof(nt_prstatus));
375 	ptr = nt_init(ptr, NT_PRFPREG, &nt_fpregset, sizeof(nt_fpregset));
376 	ptr = nt_init(ptr, NT_S390_TIMER, &sa->timer, sizeof(sa->timer));
377 	ptr = nt_init(ptr, NT_S390_TODCMP, &sa->todcmp, sizeof(sa->todcmp));
378 	ptr = nt_init(ptr, NT_S390_TODPREG, &sa->todpreg, sizeof(sa->todpreg));
379 	ptr = nt_init(ptr, NT_S390_CTRS, &sa->ctrs, sizeof(sa->ctrs));
380 	ptr = nt_init(ptr, NT_S390_PREFIX, &sa->prefix, sizeof(sa->prefix));
381 	if (MACHINE_HAS_VX) {
382 		ptr = nt_init(ptr, NT_S390_VXRS_HIGH,
383 			      &sa->vxrs_high, sizeof(sa->vxrs_high));
384 		ptr = nt_init(ptr, NT_S390_VXRS_LOW,
385 			      &sa->vxrs_low, sizeof(sa->vxrs_low));
386 	}
387 	return ptr;
388 }
389 
390 /*
391  * Calculate size of ELF notes per cpu
392  */
393 static size_t get_cpu_elf_notes_size(void)
394 {
395 	struct save_area *sa = NULL;
396 	size_t size;
397 
398 	size =	nt_size(NT_PRSTATUS, sizeof(struct elf_prstatus));
399 	size +=  nt_size(NT_PRFPREG, sizeof(elf_fpregset_t));
400 	size +=  nt_size(NT_S390_TIMER, sizeof(sa->timer));
401 	size +=  nt_size(NT_S390_TODCMP, sizeof(sa->todcmp));
402 	size +=  nt_size(NT_S390_TODPREG, sizeof(sa->todpreg));
403 	size +=  nt_size(NT_S390_CTRS, sizeof(sa->ctrs));
404 	size +=  nt_size(NT_S390_PREFIX, sizeof(sa->prefix));
405 	if (MACHINE_HAS_VX) {
406 		size += nt_size(NT_S390_VXRS_HIGH, sizeof(sa->vxrs_high));
407 		size += nt_size(NT_S390_VXRS_LOW, sizeof(sa->vxrs_low));
408 	}
409 
410 	return size;
411 }
412 
413 /*
414  * Initialize prpsinfo note (new kernel)
415  */
416 static void *nt_prpsinfo(void *ptr)
417 {
418 	struct elf_prpsinfo prpsinfo;
419 
420 	memset(&prpsinfo, 0, sizeof(prpsinfo));
421 	prpsinfo.pr_sname = 'R';
422 	strcpy(prpsinfo.pr_fname, "vmlinux");
423 	return nt_init(ptr, NT_PRPSINFO, &prpsinfo, sizeof(prpsinfo));
424 }
425 
426 /*
427  * Get vmcoreinfo using lowcore->vmcore_info (new kernel)
428  */
429 static void *get_vmcoreinfo_old(unsigned long *size)
430 {
431 	char nt_name[11], *vmcoreinfo;
432 	Elf64_Nhdr note;
433 	void *addr;
434 
435 	if (copy_oldmem_kernel(&addr, &S390_lowcore.vmcore_info, sizeof(addr)))
436 		return NULL;
437 	memset(nt_name, 0, sizeof(nt_name));
438 	if (copy_oldmem_kernel(&note, addr, sizeof(note)))
439 		return NULL;
440 	if (copy_oldmem_kernel(nt_name, addr + sizeof(note),
441 			       sizeof(nt_name) - 1))
442 		return NULL;
443 	if (strcmp(nt_name, VMCOREINFO_NOTE_NAME) != 0)
444 		return NULL;
445 	vmcoreinfo = kzalloc(note.n_descsz, GFP_KERNEL);
446 	if (!vmcoreinfo)
447 		return NULL;
448 	if (copy_oldmem_kernel(vmcoreinfo, addr + 24, note.n_descsz)) {
449 		kfree(vmcoreinfo);
450 		return NULL;
451 	}
452 	*size = note.n_descsz;
453 	return vmcoreinfo;
454 }
455 
456 /*
457  * Initialize vmcoreinfo note (new kernel)
458  */
459 static void *nt_vmcoreinfo(void *ptr)
460 {
461 	const char *name = VMCOREINFO_NOTE_NAME;
462 	unsigned long size;
463 	void *vmcoreinfo;
464 
465 	vmcoreinfo = os_info_old_entry(OS_INFO_VMCOREINFO, &size);
466 	if (vmcoreinfo)
467 		return nt_init_name(ptr, 0, vmcoreinfo, size, name);
468 
469 	vmcoreinfo = get_vmcoreinfo_old(&size);
470 	if (!vmcoreinfo)
471 		return ptr;
472 	ptr = nt_init_name(ptr, 0, vmcoreinfo, size, name);
473 	kfree(vmcoreinfo);
474 	return ptr;
475 }
476 
477 static size_t nt_vmcoreinfo_size(void)
478 {
479 	const char *name = VMCOREINFO_NOTE_NAME;
480 	unsigned long size;
481 	void *vmcoreinfo;
482 
483 	vmcoreinfo = os_info_old_entry(OS_INFO_VMCOREINFO, &size);
484 	if (vmcoreinfo)
485 		return nt_size_name(size, name);
486 
487 	vmcoreinfo = get_vmcoreinfo_old(&size);
488 	if (!vmcoreinfo)
489 		return 0;
490 
491 	kfree(vmcoreinfo);
492 	return nt_size_name(size, name);
493 }
494 
495 /*
496  * Initialize final note (needed for /proc/vmcore code)
497  */
498 static void *nt_final(void *ptr)
499 {
500 	Elf64_Nhdr *note;
501 
502 	note = (Elf64_Nhdr *) ptr;
503 	note->n_namesz = 0;
504 	note->n_descsz = 0;
505 	note->n_type = 0;
506 	return PTR_ADD(ptr, sizeof(Elf64_Nhdr));
507 }
508 
509 /*
510  * Initialize ELF header (new kernel)
511  */
512 static void *ehdr_init(Elf64_Ehdr *ehdr, int mem_chunk_cnt)
513 {
514 	memset(ehdr, 0, sizeof(*ehdr));
515 	memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
516 	ehdr->e_ident[EI_CLASS] = ELFCLASS64;
517 	ehdr->e_ident[EI_DATA] = ELFDATA2MSB;
518 	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
519 	memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
520 	ehdr->e_type = ET_CORE;
521 	ehdr->e_machine = EM_S390;
522 	ehdr->e_version = EV_CURRENT;
523 	ehdr->e_phoff = sizeof(Elf64_Ehdr);
524 	ehdr->e_ehsize = sizeof(Elf64_Ehdr);
525 	ehdr->e_phentsize = sizeof(Elf64_Phdr);
526 	ehdr->e_phnum = mem_chunk_cnt + 1;
527 	return ehdr + 1;
528 }
529 
530 /*
531  * Return CPU count for ELF header (new kernel)
532  */
533 static int get_cpu_cnt(void)
534 {
535 	struct save_area *sa;
536 	int cpus = 0;
537 
538 	list_for_each_entry(sa, &dump_save_areas, list)
539 		if (sa->prefix != 0)
540 			cpus++;
541 	return cpus;
542 }
543 
544 /*
545  * Return memory chunk count for ELF header (new kernel)
546  */
547 static int get_mem_chunk_cnt(void)
548 {
549 	int cnt = 0;
550 	u64 idx;
551 
552 	for_each_mem_range(idx, &memblock.physmem, &oldmem_type, NUMA_NO_NODE,
553 			   MEMBLOCK_NONE, NULL, 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_mem_range(idx, &memblock.physmem, &oldmem_type, NUMA_NO_NODE,
567 			   MEMBLOCK_NONE, &start, &end, NULL) {
568 		phdr->p_filesz = end - start;
569 		phdr->p_type = PT_LOAD;
570 		phdr->p_offset = start;
571 		phdr->p_vaddr = start;
572 		phdr->p_paddr = start;
573 		phdr->p_memsz = end - start;
574 		phdr->p_flags = PF_R | PF_W | PF_X;
575 		phdr->p_align = PAGE_SIZE;
576 		phdr++;
577 	}
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 mem_chunk_cnt)
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 += mem_chunk_cnt * 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;
632 	int mem_chunk_cnt;
633 	void *ptr, *hdr;
634 	u32 alloc_size;
635 	u64 hdr_off;
636 
637 	/* If we are not in kdump or zfcpdump mode return */
638 	if (!OLDMEM_BASE && ipl_info.type != IPL_TYPE_FCP_DUMP)
639 		return 0;
640 	/* If we cannot get HSA size for zfcpdump return error */
641 	if (ipl_info.type == IPL_TYPE_FCP_DUMP && !sclp.hsa_size)
642 		return -ENODEV;
643 
644 	/* For kdump, exclude previous crashkernel memory */
645 	if (OLDMEM_BASE) {
646 		oldmem_region.base = OLDMEM_BASE;
647 		oldmem_region.size = OLDMEM_SIZE;
648 		oldmem_type.total_size = OLDMEM_SIZE;
649 	}
650 
651 	mem_chunk_cnt = get_mem_chunk_cnt();
652 
653 	alloc_size = get_elfcorehdr_size(mem_chunk_cnt);
654 
655 	hdr = kzalloc(alloc_size, GFP_KERNEL);
656 
657 	/* Without elfcorehdr /proc/vmcore cannot be created. Thus creating
658 	 * a dump with this crash kernel will fail. Panic now to allow other
659 	 * dump mechanisms to take over.
660 	 */
661 	if (!hdr)
662 		panic("s390 kdump allocating elfcorehdr failed");
663 
664 	/* Init elf header */
665 	ptr = ehdr_init(hdr, mem_chunk_cnt);
666 	/* Init program headers */
667 	phdr_notes = ptr;
668 	ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr));
669 	phdr_loads = ptr;
670 	ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr) * mem_chunk_cnt);
671 	/* Init notes */
672 	hdr_off = PTR_DIFF(ptr, hdr);
673 	ptr = notes_init(phdr_notes, ptr, ((unsigned long) hdr) + hdr_off);
674 	/* Init loads */
675 	hdr_off = PTR_DIFF(ptr, hdr);
676 	loads_init(phdr_loads, hdr_off);
677 	*addr = (unsigned long long) hdr;
678 	*size = (unsigned long long) hdr_off;
679 	BUG_ON(elfcorehdr_size > alloc_size);
680 	return 0;
681 }
682 
683 /*
684  * Free ELF core header (new kernel)
685  */
686 void elfcorehdr_free(unsigned long long addr)
687 {
688 	kfree((void *)(unsigned long)addr);
689 }
690 
691 /*
692  * Read from ELF header
693  */
694 ssize_t elfcorehdr_read(char *buf, size_t count, u64 *ppos)
695 {
696 	void *src = (void *)(unsigned long)*ppos;
697 
698 	memcpy(buf, src, count);
699 	*ppos += count;
700 	return count;
701 }
702 
703 /*
704  * Read from ELF notes data
705  */
706 ssize_t elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos)
707 {
708 	void *src = (void *)(unsigned long)*ppos;
709 
710 	memcpy(buf, src, count);
711 	*ppos += count;
712 	return count;
713 }
714