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  */
save_area_alloc(bool is_boot_cpu)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  */
save_area_boot_cpu(void)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  */
save_area_add_regs(struct save_area * sa,void * regs)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  */
save_area_add_vxrs(struct save_area * sa,__vector128 * vxrs)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 
copy_oldmem_iter(struct iov_iter * iter,unsigned long src,size_t count)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 
copy_oldmem_kernel(void * dst,unsigned long src,size_t count)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  */
copy_oldmem_page(struct iov_iter * iter,unsigned long pfn,size_t csize,unsigned long offset)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  */
remap_oldmem_pfn_range_kdump(struct vm_area_struct * vma,unsigned long from,unsigned long pfn,unsigned long size,pgprot_t prot)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  */
remap_oldmem_pfn_range_zfcpdump(struct vm_area_struct * vma,unsigned long from,unsigned long pfn,unsigned long size,pgprot_t prot)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  */
remap_oldmem_pfn_range(struct vm_area_struct * vma,unsigned long from,unsigned long pfn,unsigned long size,pgprot_t prot)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  */
is_kdump_kernel(void)243 bool is_kdump_kernel(void)
244 {
245 	return oldmem_data.start;
246 }
247 EXPORT_SYMBOL_GPL(is_kdump_kernel);
248 
249 /*
250  * Initialize ELF note
251  */
nt_init_name(void * buf,Elf64_Word type,void * desc,int d_len,const char * name)252 static void *nt_init_name(void *buf, Elf64_Word type, void *desc, int d_len,
253 			  const char *name)
254 {
255 	Elf64_Nhdr *note;
256 	u64 len;
257 
258 	note = (Elf64_Nhdr *)buf;
259 	note->n_namesz = strlen(name) + 1;
260 	note->n_descsz = d_len;
261 	note->n_type = type;
262 	len = sizeof(Elf64_Nhdr);
263 
264 	memcpy(buf + len, name, note->n_namesz);
265 	len = roundup(len + note->n_namesz, 4);
266 
267 	memcpy(buf + len, desc, note->n_descsz);
268 	len = roundup(len + note->n_descsz, 4);
269 
270 	return PTR_ADD(buf, len);
271 }
272 
273 #define nt_init(buf, type, desc) \
274 	nt_init_name(buf, NT_ ## type, &(desc), sizeof(desc), NN_ ## type)
275 
276 /*
277  * Calculate the size of ELF note
278  */
nt_size_name(int d_len,const char * name)279 static size_t nt_size_name(int d_len, const char *name)
280 {
281 	size_t size;
282 
283 	size = sizeof(Elf64_Nhdr);
284 	size += roundup(strlen(name) + 1, 4);
285 	size += roundup(d_len, 4);
286 
287 	return size;
288 }
289 
290 #define nt_size(type, desc) nt_size_name(sizeof(desc), NN_ ## type)
291 
292 /*
293  * Fill ELF notes for one CPU with save area registers
294  */
fill_cpu_elf_notes(void * ptr,int cpu,struct save_area * sa)295 static void *fill_cpu_elf_notes(void *ptr, int cpu, struct save_area *sa)
296 {
297 	struct elf_prstatus nt_prstatus;
298 	elf_fpregset_t nt_fpregset;
299 
300 	/* Prepare prstatus note */
301 	memset(&nt_prstatus, 0, sizeof(nt_prstatus));
302 	memcpy(&nt_prstatus.pr_reg.gprs, sa->gprs, sizeof(sa->gprs));
303 	memcpy(&nt_prstatus.pr_reg.psw, sa->psw, sizeof(sa->psw));
304 	memcpy(&nt_prstatus.pr_reg.acrs, sa->acrs, sizeof(sa->acrs));
305 	nt_prstatus.common.pr_pid = cpu;
306 	/* Prepare fpregset (floating point) note */
307 	memset(&nt_fpregset, 0, sizeof(nt_fpregset));
308 	memcpy(&nt_fpregset.fpc, &sa->fpc, sizeof(sa->fpc));
309 	memcpy(&nt_fpregset.fprs, &sa->fprs, sizeof(sa->fprs));
310 	/* Create ELF notes for the CPU */
311 	ptr = nt_init(ptr, PRSTATUS, nt_prstatus);
312 	ptr = nt_init(ptr, PRFPREG, nt_fpregset);
313 	ptr = nt_init(ptr, S390_TIMER, sa->timer);
314 	ptr = nt_init(ptr, S390_TODCMP, sa->todcmp);
315 	ptr = nt_init(ptr, S390_TODPREG, sa->todpreg);
316 	ptr = nt_init(ptr, S390_CTRS, sa->ctrs);
317 	ptr = nt_init(ptr, S390_PREFIX, sa->prefix);
318 	if (cpu_has_vx()) {
319 		ptr = nt_init(ptr, S390_VXRS_HIGH, sa->vxrs_high);
320 		ptr = nt_init(ptr, S390_VXRS_LOW, sa->vxrs_low);
321 	}
322 	return ptr;
323 }
324 
325 /*
326  * Calculate size of ELF notes per cpu
327  */
get_cpu_elf_notes_size(void)328 static size_t get_cpu_elf_notes_size(void)
329 {
330 	struct save_area *sa = NULL;
331 	size_t size;
332 
333 	size =	nt_size(PRSTATUS, struct elf_prstatus);
334 	size += nt_size(PRFPREG, elf_fpregset_t);
335 	size += nt_size(S390_TIMER, sa->timer);
336 	size += nt_size(S390_TODCMP, sa->todcmp);
337 	size += nt_size(S390_TODPREG, sa->todpreg);
338 	size += nt_size(S390_CTRS, sa->ctrs);
339 	size += nt_size(S390_PREFIX, sa->prefix);
340 	if (cpu_has_vx()) {
341 		size += nt_size(S390_VXRS_HIGH, sa->vxrs_high);
342 		size += nt_size(S390_VXRS_LOW, sa->vxrs_low);
343 	}
344 
345 	return size;
346 }
347 
348 /*
349  * Initialize prpsinfo note (new kernel)
350  */
nt_prpsinfo(void * ptr)351 static void *nt_prpsinfo(void *ptr)
352 {
353 	struct elf_prpsinfo prpsinfo;
354 
355 	memset(&prpsinfo, 0, sizeof(prpsinfo));
356 	prpsinfo.pr_sname = 'R';
357 	strcpy(prpsinfo.pr_fname, "vmlinux");
358 	return nt_init(ptr, PRPSINFO, prpsinfo);
359 }
360 
361 /*
362  * Get vmcoreinfo using lowcore->vmcore_info (new kernel)
363  */
get_vmcoreinfo_old(unsigned long * size)364 static void *get_vmcoreinfo_old(unsigned long *size)
365 {
366 	char nt_name[11], *vmcoreinfo;
367 	unsigned long addr;
368 	Elf64_Nhdr note;
369 
370 	if (copy_oldmem_kernel(&addr, __LC_VMCORE_INFO, sizeof(addr)))
371 		return NULL;
372 	memset(nt_name, 0, sizeof(nt_name));
373 	if (copy_oldmem_kernel(&note, addr, sizeof(note)))
374 		return NULL;
375 	if (copy_oldmem_kernel(nt_name, addr + sizeof(note),
376 			       sizeof(nt_name) - 1))
377 		return NULL;
378 	if (strcmp(nt_name, VMCOREINFO_NOTE_NAME) != 0)
379 		return NULL;
380 	vmcoreinfo = kzalloc(note.n_descsz, GFP_KERNEL);
381 	if (!vmcoreinfo)
382 		return NULL;
383 	if (copy_oldmem_kernel(vmcoreinfo, addr + 24, note.n_descsz)) {
384 		kfree(vmcoreinfo);
385 		return NULL;
386 	}
387 	*size = note.n_descsz;
388 	return vmcoreinfo;
389 }
390 
391 /*
392  * Initialize vmcoreinfo note (new kernel)
393  */
nt_vmcoreinfo(void * ptr)394 static void *nt_vmcoreinfo(void *ptr)
395 {
396 	const char *name = VMCOREINFO_NOTE_NAME;
397 	unsigned long size;
398 	void *vmcoreinfo;
399 
400 	vmcoreinfo = os_info_old_entry(OS_INFO_VMCOREINFO, &size);
401 	if (vmcoreinfo)
402 		return nt_init_name(ptr, 0, vmcoreinfo, size, name);
403 
404 	vmcoreinfo = get_vmcoreinfo_old(&size);
405 	if (!vmcoreinfo)
406 		return ptr;
407 	ptr = nt_init_name(ptr, 0, vmcoreinfo, size, name);
408 	kfree(vmcoreinfo);
409 	return ptr;
410 }
411 
nt_vmcoreinfo_size(void)412 static size_t nt_vmcoreinfo_size(void)
413 {
414 	const char *name = VMCOREINFO_NOTE_NAME;
415 	unsigned long size;
416 	void *vmcoreinfo;
417 
418 	vmcoreinfo = os_info_old_entry(OS_INFO_VMCOREINFO, &size);
419 	if (vmcoreinfo)
420 		return nt_size_name(size, name);
421 
422 	vmcoreinfo = get_vmcoreinfo_old(&size);
423 	if (!vmcoreinfo)
424 		return 0;
425 
426 	kfree(vmcoreinfo);
427 	return nt_size_name(size, name);
428 }
429 
430 /*
431  * Initialize final note (needed for /proc/vmcore code)
432  */
nt_final(void * ptr)433 static void *nt_final(void *ptr)
434 {
435 	Elf64_Nhdr *note;
436 
437 	note = (Elf64_Nhdr *) ptr;
438 	note->n_namesz = 0;
439 	note->n_descsz = 0;
440 	note->n_type = 0;
441 	return PTR_ADD(ptr, sizeof(Elf64_Nhdr));
442 }
443 
444 /*
445  * Initialize ELF header (new kernel)
446  */
ehdr_init(Elf64_Ehdr * ehdr,int phdr_count)447 static void *ehdr_init(Elf64_Ehdr *ehdr, int phdr_count)
448 {
449 	memset(ehdr, 0, sizeof(*ehdr));
450 	memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
451 	ehdr->e_ident[EI_CLASS] = ELFCLASS64;
452 	ehdr->e_ident[EI_DATA] = ELFDATA2MSB;
453 	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
454 	memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
455 	ehdr->e_type = ET_CORE;
456 	ehdr->e_machine = EM_S390;
457 	ehdr->e_version = EV_CURRENT;
458 	ehdr->e_phoff = sizeof(Elf64_Ehdr);
459 	ehdr->e_ehsize = sizeof(Elf64_Ehdr);
460 	ehdr->e_phentsize = sizeof(Elf64_Phdr);
461 	/* Number of PT_LOAD program headers plus PT_NOTE program header */
462 	ehdr->e_phnum = phdr_count + 1;
463 	return ehdr + 1;
464 }
465 
466 /*
467  * Return CPU count for ELF header (new kernel)
468  */
get_cpu_cnt(void)469 static int get_cpu_cnt(void)
470 {
471 	struct save_area *sa;
472 	int cpus = 0;
473 
474 	list_for_each_entry(sa, &dump_save_areas, list)
475 		if (sa->prefix != 0)
476 			cpus++;
477 	return cpus;
478 }
479 
480 /*
481  * Return memory chunk count for ELF header (new kernel)
482  */
get_mem_chunk_cnt(void)483 static int get_mem_chunk_cnt(void)
484 {
485 	int cnt = 0;
486 	u64 idx;
487 
488 	for_each_physmem_range(idx, &oldmem_type, NULL, NULL)
489 		cnt++;
490 	return cnt;
491 }
492 
fill_ptload(Elf64_Phdr * phdr,unsigned long paddr,unsigned long vaddr,unsigned long size)493 static void fill_ptload(Elf64_Phdr *phdr, unsigned long paddr,
494 		unsigned long vaddr, unsigned long size)
495 {
496 	phdr->p_type = PT_LOAD;
497 	phdr->p_vaddr = vaddr;
498 	phdr->p_offset = paddr;
499 	phdr->p_paddr = paddr;
500 	phdr->p_filesz = size;
501 	phdr->p_memsz = size;
502 	phdr->p_flags = PF_R | PF_W | PF_X;
503 	phdr->p_align = PAGE_SIZE;
504 }
505 
506 /*
507  * Initialize ELF loads (new kernel)
508  */
loads_init(Elf64_Phdr * phdr,bool os_info_has_vm)509 static void loads_init(Elf64_Phdr *phdr, bool os_info_has_vm)
510 {
511 	unsigned long old_identity_base = 0;
512 	phys_addr_t start, end;
513 	u64 idx;
514 
515 	if (os_info_has_vm)
516 		old_identity_base = os_info_old_value(OS_INFO_IDENTITY_BASE);
517 	for_each_physmem_range(idx, &oldmem_type, &start, &end) {
518 		fill_ptload(phdr, start, old_identity_base + start,
519 			    end - start);
520 		phdr++;
521 	}
522 }
523 
os_info_has_vm(void)524 static bool os_info_has_vm(void)
525 {
526 	return os_info_old_value(OS_INFO_KASLR_OFFSET);
527 }
528 
529 #ifdef CONFIG_PROC_VMCORE_DEVICE_RAM
530 /*
531  * Fill PT_LOAD for a physical memory range owned by a device and detected by
532  * its device driver.
533  */
elfcorehdr_fill_device_ram_ptload_elf64(Elf64_Phdr * phdr,unsigned long long paddr,unsigned long long size)534 void elfcorehdr_fill_device_ram_ptload_elf64(Elf64_Phdr *phdr,
535 		unsigned long long paddr, unsigned long long size)
536 {
537 	unsigned long old_identity_base = 0;
538 
539 	if (os_info_has_vm())
540 		old_identity_base = os_info_old_value(OS_INFO_IDENTITY_BASE);
541 	fill_ptload(phdr, paddr, old_identity_base + paddr, size);
542 }
543 #endif
544 
545 /*
546  * Prepare PT_LOAD type program header for kernel image region
547  */
text_init(Elf64_Phdr * phdr)548 static void text_init(Elf64_Phdr *phdr)
549 {
550 	unsigned long start_phys = os_info_old_value(OS_INFO_IMAGE_PHYS);
551 	unsigned long start = os_info_old_value(OS_INFO_IMAGE_START);
552 	unsigned long end = os_info_old_value(OS_INFO_IMAGE_END);
553 
554 	phdr->p_type = PT_LOAD;
555 	phdr->p_vaddr = start;
556 	phdr->p_filesz = end - start;
557 	phdr->p_memsz = end - start;
558 	phdr->p_offset = start_phys;
559 	phdr->p_paddr = start_phys;
560 	phdr->p_flags = PF_R | PF_W | PF_X;
561 	phdr->p_align = PAGE_SIZE;
562 }
563 
564 /*
565  * Initialize notes (new kernel)
566  */
notes_init(Elf64_Phdr * phdr,void * ptr,u64 notes_offset)567 static void *notes_init(Elf64_Phdr *phdr, void *ptr, u64 notes_offset)
568 {
569 	struct save_area *sa;
570 	void *ptr_start = ptr;
571 	int cpu;
572 
573 	ptr = nt_prpsinfo(ptr);
574 
575 	cpu = 1;
576 	list_for_each_entry(sa, &dump_save_areas, list)
577 		if (sa->prefix != 0)
578 			ptr = fill_cpu_elf_notes(ptr, cpu++, sa);
579 	ptr = nt_vmcoreinfo(ptr);
580 	ptr = nt_final(ptr);
581 	memset(phdr, 0, sizeof(*phdr));
582 	phdr->p_type = PT_NOTE;
583 	phdr->p_offset = notes_offset;
584 	phdr->p_filesz = (unsigned long) PTR_SUB(ptr, ptr_start);
585 	phdr->p_memsz = phdr->p_filesz;
586 	return ptr;
587 }
588 
get_elfcorehdr_size(int phdr_count)589 static size_t get_elfcorehdr_size(int phdr_count)
590 {
591 	size_t size;
592 
593 	size = sizeof(Elf64_Ehdr);
594 	/* PT_NOTES */
595 	size += sizeof(Elf64_Phdr);
596 	/* nt_prpsinfo */
597 	size += nt_size(PRPSINFO, struct elf_prpsinfo);
598 	/* regsets */
599 	size += get_cpu_cnt() * get_cpu_elf_notes_size();
600 	/* nt_vmcoreinfo */
601 	size += nt_vmcoreinfo_size();
602 	/* nt_final */
603 	size += sizeof(Elf64_Nhdr);
604 	/* PT_LOADS */
605 	size += phdr_count * sizeof(Elf64_Phdr);
606 
607 	return size;
608 }
609 
610 /*
611  * Create ELF core header (new kernel)
612  */
elfcorehdr_alloc(unsigned long long * addr,unsigned long long * size)613 int elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size)
614 {
615 	Elf64_Phdr *phdr_notes, *phdr_loads, *phdr_text;
616 	int mem_chunk_cnt, phdr_text_cnt;
617 	size_t alloc_size;
618 	void *ptr, *hdr;
619 	u64 hdr_off;
620 
621 	/* If we are not in kdump or zfcp/nvme dump mode return */
622 	if (!oldmem_data.start && !is_ipl_type_dump())
623 		return 0;
624 	/* If we cannot get HSA size for zfcp/nvme dump return error */
625 	if (is_ipl_type_dump() && !sclp.hsa_size)
626 		return -ENODEV;
627 
628 	/* For kdump, exclude previous crashkernel memory */
629 	if (oldmem_data.start) {
630 		oldmem_region.base = oldmem_data.start;
631 		oldmem_region.size = oldmem_data.size;
632 		oldmem_type.total_size = oldmem_data.size;
633 	}
634 
635 	mem_chunk_cnt = get_mem_chunk_cnt();
636 	phdr_text_cnt = os_info_has_vm() ? 1 : 0;
637 
638 	alloc_size = get_elfcorehdr_size(mem_chunk_cnt + phdr_text_cnt);
639 
640 	hdr = kzalloc(alloc_size, GFP_KERNEL);
641 
642 	/*
643 	 * Without elfcorehdr /proc/vmcore cannot be created. Thus creating
644 	 * a dump with this crash kernel will fail. Panic now to allow other
645 	 * dump mechanisms to take over.
646 	 */
647 	if (!hdr)
648 		panic("s390 kdump allocating elfcorehdr failed");
649 
650 	/* Init elf header */
651 	phdr_notes = ehdr_init(hdr, mem_chunk_cnt + phdr_text_cnt);
652 	/* Init program headers */
653 	if (phdr_text_cnt) {
654 		phdr_text = phdr_notes + 1;
655 		phdr_loads = phdr_text + 1;
656 	} else {
657 		phdr_loads = phdr_notes + 1;
658 	}
659 	ptr = PTR_ADD(phdr_loads, sizeof(Elf64_Phdr) * mem_chunk_cnt);
660 	/* Init notes */
661 	hdr_off = PTR_DIFF(ptr, hdr);
662 	ptr = notes_init(phdr_notes, ptr, ((unsigned long) hdr) + hdr_off);
663 	/* Init kernel text program header */
664 	if (phdr_text_cnt)
665 		text_init(phdr_text);
666 	/* Init loads */
667 	loads_init(phdr_loads, phdr_text_cnt);
668 	/* Finalize program headers */
669 	hdr_off = PTR_DIFF(ptr, hdr);
670 	*addr = (unsigned long long) hdr;
671 	*size = (unsigned long long) hdr_off;
672 	BUG_ON(elfcorehdr_size > alloc_size);
673 	return 0;
674 }
675 
676 /*
677  * Free ELF core header (new kernel)
678  */
elfcorehdr_free(unsigned long long addr)679 void elfcorehdr_free(unsigned long long addr)
680 {
681 	kfree((void *)(unsigned long)addr);
682 }
683 
684 /*
685  * Read from ELF header
686  */
elfcorehdr_read(char * buf,size_t count,u64 * ppos)687 ssize_t elfcorehdr_read(char *buf, size_t count, u64 *ppos)
688 {
689 	void *src = (void *)(unsigned long)*ppos;
690 
691 	memcpy(buf, src, count);
692 	*ppos += count;
693 	return count;
694 }
695 
696 /*
697  * Read from ELF notes data
698  */
elfcorehdr_read_notes(char * buf,size_t count,u64 * ppos)699 ssize_t elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos)
700 {
701 	void *src = (void *)(unsigned long)*ppos;
702 
703 	memcpy(buf, src, count);
704 	*ppos += count;
705 	return count;
706 }
707