xref: /linux/arch/s390/kernel/crash_dump.c (revision c1aac62f36c1e37ee81c9e09ee9ee733eef05dcb)
1 /*
2  * S390 kdump implementation
3  *
4  * Copyright IBM Corp. 2011
5  * Author(s): Michael Holzheu <holzheu@linux.vnet.ibm.com>
6  */
7 
8 #include <linux/crash_dump.h>
9 #include <asm/lowcore.h>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/mm.h>
13 #include <linux/gfp.h>
14 #include <linux/slab.h>
15 #include <linux/bootmem.h>
16 #include <linux/elf.h>
17 #include <asm/asm-offsets.h>
18 #include <linux/memblock.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 };
36 
37 struct save_area {
38 	struct list_head list;
39 	u64 psw[2];
40 	u64 ctrs[16];
41 	u64 gprs[16];
42 	u32 acrs[16];
43 	u64 fprs[16];
44 	u32 fpc;
45 	u32 prefix;
46 	u64 todpreg;
47 	u64 timer;
48 	u64 todcmp;
49 	u64 vxrs_low[16];
50 	__vector128 vxrs_high[16];
51 };
52 
53 static LIST_HEAD(dump_save_areas);
54 
55 /*
56  * Allocate a save area
57  */
58 struct save_area * __init save_area_alloc(bool is_boot_cpu)
59 {
60 	struct save_area *sa;
61 
62 	sa = (void *) memblock_alloc(sizeof(*sa), 8);
63 	if (is_boot_cpu)
64 		list_add(&sa->list, &dump_save_areas);
65 	else
66 		list_add_tail(&sa->list, &dump_save_areas);
67 	return sa;
68 }
69 
70 /*
71  * Return the address of the save area for the boot CPU
72  */
73 struct save_area * __init save_area_boot_cpu(void)
74 {
75 	return list_first_entry_or_null(&dump_save_areas, struct save_area, list);
76 }
77 
78 /*
79  * Copy CPU registers into the save area
80  */
81 void __init save_area_add_regs(struct save_area *sa, void *regs)
82 {
83 	struct lowcore *lc;
84 
85 	lc = (struct lowcore *)(regs - __LC_FPREGS_SAVE_AREA);
86 	memcpy(&sa->psw, &lc->psw_save_area, sizeof(sa->psw));
87 	memcpy(&sa->ctrs, &lc->cregs_save_area, sizeof(sa->ctrs));
88 	memcpy(&sa->gprs, &lc->gpregs_save_area, sizeof(sa->gprs));
89 	memcpy(&sa->acrs, &lc->access_regs_save_area, sizeof(sa->acrs));
90 	memcpy(&sa->fprs, &lc->floating_pt_save_area, sizeof(sa->fprs));
91 	memcpy(&sa->fpc, &lc->fpt_creg_save_area, sizeof(sa->fpc));
92 	memcpy(&sa->prefix, &lc->prefixreg_save_area, sizeof(sa->prefix));
93 	memcpy(&sa->todpreg, &lc->tod_progreg_save_area, sizeof(sa->todpreg));
94 	memcpy(&sa->timer, &lc->cpu_timer_save_area, sizeof(sa->timer));
95 	memcpy(&sa->todcmp, &lc->clock_comp_save_area, sizeof(sa->todcmp));
96 }
97 
98 /*
99  * Copy vector registers into the save area
100  */
101 void __init save_area_add_vxrs(struct save_area *sa, __vector128 *vxrs)
102 {
103 	int i;
104 
105 	/* Copy lower halves of vector registers 0-15 */
106 	for (i = 0; i < 16; i++)
107 		memcpy(&sa->vxrs_low[i], &vxrs[i].u[2], 8);
108 	/* Copy vector registers 16-31 */
109 	memcpy(sa->vxrs_high, vxrs + 16, 16 * sizeof(__vector128));
110 }
111 
112 /*
113  * Return physical address for virtual address
114  */
115 static inline void *load_real_addr(void *addr)
116 {
117 	unsigned long real_addr;
118 
119 	asm volatile(
120 		   "	lra     %0,0(%1)\n"
121 		   "	jz	0f\n"
122 		   "	la	%0,0\n"
123 		   "0:"
124 		   : "=a" (real_addr) : "a" (addr) : "cc");
125 	return (void *)real_addr;
126 }
127 
128 /*
129  * Copy memory of the old, dumped system to a kernel space virtual address
130  */
131 int copy_oldmem_kernel(void *dst, void *src, size_t count)
132 {
133 	unsigned long from, len;
134 	void *ra;
135 	int rc;
136 
137 	while (count) {
138 		from = __pa(src);
139 		if (!OLDMEM_BASE && from < sclp.hsa_size) {
140 			/* Copy from zfcpdump HSA area */
141 			len = min(count, sclp.hsa_size - from);
142 			rc = memcpy_hsa_kernel(dst, from, len);
143 			if (rc)
144 				return rc;
145 		} else {
146 			/* Check for swapped kdump oldmem areas */
147 			if (OLDMEM_BASE && from - OLDMEM_BASE < OLDMEM_SIZE) {
148 				from -= OLDMEM_BASE;
149 				len = min(count, OLDMEM_SIZE - from);
150 			} else if (OLDMEM_BASE && from < OLDMEM_SIZE) {
151 				len = min(count, OLDMEM_SIZE - from);
152 				from += OLDMEM_BASE;
153 			} else {
154 				len = count;
155 			}
156 			if (is_vmalloc_or_module_addr(dst)) {
157 				ra = load_real_addr(dst);
158 				len = min(PAGE_SIZE - offset_in_page(ra), len);
159 			} else {
160 				ra = dst;
161 			}
162 			if (memcpy_real(ra, (void *) from, len))
163 				return -EFAULT;
164 		}
165 		dst += len;
166 		src += len;
167 		count -= len;
168 	}
169 	return 0;
170 }
171 
172 /*
173  * Copy memory of the old, dumped system to a user space virtual address
174  */
175 static int copy_oldmem_user(void __user *dst, void *src, size_t count)
176 {
177 	unsigned long from, len;
178 	int rc;
179 
180 	while (count) {
181 		from = __pa(src);
182 		if (!OLDMEM_BASE && from < sclp.hsa_size) {
183 			/* Copy from zfcpdump HSA area */
184 			len = min(count, sclp.hsa_size - from);
185 			rc = memcpy_hsa_user(dst, from, len);
186 			if (rc)
187 				return rc;
188 		} else {
189 			/* Check for swapped kdump oldmem areas */
190 			if (OLDMEM_BASE && from - OLDMEM_BASE < OLDMEM_SIZE) {
191 				from -= OLDMEM_BASE;
192 				len = min(count, OLDMEM_SIZE - from);
193 			} else if (OLDMEM_BASE && from < OLDMEM_SIZE) {
194 				len = min(count, OLDMEM_SIZE - from);
195 				from += OLDMEM_BASE;
196 			} else {
197 				len = count;
198 			}
199 			rc = copy_to_user_real(dst, (void *) from, count);
200 			if (rc)
201 				return rc;
202 		}
203 		dst += len;
204 		src += len;
205 		count -= len;
206 	}
207 	return 0;
208 }
209 
210 /*
211  * Copy one page from "oldmem"
212  */
213 ssize_t copy_oldmem_page(unsigned long pfn, char *buf, size_t csize,
214 			 unsigned long offset, int userbuf)
215 {
216 	void *src;
217 	int rc;
218 
219 	if (!csize)
220 		return 0;
221 	src = (void *) (pfn << PAGE_SHIFT) + offset;
222 	if (userbuf)
223 		rc = copy_oldmem_user((void __force __user *) buf, src, csize);
224 	else
225 		rc = copy_oldmem_kernel((void *) buf, src, csize);
226 	return rc;
227 }
228 
229 /*
230  * Remap "oldmem" for kdump
231  *
232  * For the kdump reserved memory this functions performs a swap operation:
233  * [0 - OLDMEM_SIZE] is mapped to [OLDMEM_BASE - OLDMEM_BASE + OLDMEM_SIZE]
234  */
235 static int remap_oldmem_pfn_range_kdump(struct vm_area_struct *vma,
236 					unsigned long from, unsigned long pfn,
237 					unsigned long size, pgprot_t prot)
238 {
239 	unsigned long size_old;
240 	int rc;
241 
242 	if (pfn < OLDMEM_SIZE >> PAGE_SHIFT) {
243 		size_old = min(size, OLDMEM_SIZE - (pfn << PAGE_SHIFT));
244 		rc = remap_pfn_range(vma, from,
245 				     pfn + (OLDMEM_BASE >> PAGE_SHIFT),
246 				     size_old, prot);
247 		if (rc || size == size_old)
248 			return rc;
249 		size -= size_old;
250 		from += size_old;
251 		pfn += size_old >> PAGE_SHIFT;
252 	}
253 	return remap_pfn_range(vma, from, pfn, size, prot);
254 }
255 
256 /*
257  * Remap "oldmem" for zfcpdump
258  *
259  * We only map available memory above HSA size. Memory below HSA size
260  * is read on demand using the copy_oldmem_page() function.
261  */
262 static int remap_oldmem_pfn_range_zfcpdump(struct vm_area_struct *vma,
263 					   unsigned long from,
264 					   unsigned long pfn,
265 					   unsigned long size, pgprot_t prot)
266 {
267 	unsigned long hsa_end = sclp.hsa_size;
268 	unsigned long size_hsa;
269 
270 	if (pfn < hsa_end >> PAGE_SHIFT) {
271 		size_hsa = min(size, hsa_end - (pfn << PAGE_SHIFT));
272 		if (size == size_hsa)
273 			return 0;
274 		size -= size_hsa;
275 		from += size_hsa;
276 		pfn += size_hsa >> PAGE_SHIFT;
277 	}
278 	return remap_pfn_range(vma, from, pfn, size, prot);
279 }
280 
281 /*
282  * Remap "oldmem" for kdump or zfcpdump
283  */
284 int remap_oldmem_pfn_range(struct vm_area_struct *vma, unsigned long from,
285 			   unsigned long pfn, unsigned long size, pgprot_t prot)
286 {
287 	if (OLDMEM_BASE)
288 		return remap_oldmem_pfn_range_kdump(vma, from, pfn, size, prot);
289 	else
290 		return remap_oldmem_pfn_range_zfcpdump(vma, from, pfn, size,
291 						       prot);
292 }
293 
294 /*
295  * Alloc memory and panic in case of ENOMEM
296  */
297 static void *kzalloc_panic(int len)
298 {
299 	void *rc;
300 
301 	rc = kzalloc(len, GFP_KERNEL);
302 	if (!rc)
303 		panic("s390 kdump kzalloc (%d) failed", len);
304 	return rc;
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 	const char *note_name = "LINUX";
334 
335 	if (type == NT_PRPSINFO || type == NT_PRSTATUS || type == NT_PRFPREG)
336 		note_name = KEXEC_CORE_NOTE_NAME;
337 	return nt_init_name(buf, type, desc, d_len, note_name);
338 }
339 
340 /*
341  * Fill ELF notes for one CPU with save area registers
342  */
343 static void *fill_cpu_elf_notes(void *ptr, int cpu, struct save_area *sa)
344 {
345 	struct elf_prstatus nt_prstatus;
346 	elf_fpregset_t nt_fpregset;
347 
348 	/* Prepare prstatus note */
349 	memset(&nt_prstatus, 0, sizeof(nt_prstatus));
350 	memcpy(&nt_prstatus.pr_reg.gprs, sa->gprs, sizeof(sa->gprs));
351 	memcpy(&nt_prstatus.pr_reg.psw, sa->psw, sizeof(sa->psw));
352 	memcpy(&nt_prstatus.pr_reg.acrs, sa->acrs, sizeof(sa->acrs));
353 	nt_prstatus.pr_pid = cpu;
354 	/* Prepare fpregset (floating point) note */
355 	memset(&nt_fpregset, 0, sizeof(nt_fpregset));
356 	memcpy(&nt_fpregset.fpc, &sa->fpc, sizeof(sa->fpc));
357 	memcpy(&nt_fpregset.fprs, &sa->fprs, sizeof(sa->fprs));
358 	/* Create ELF notes for the CPU */
359 	ptr = nt_init(ptr, NT_PRSTATUS, &nt_prstatus, sizeof(nt_prstatus));
360 	ptr = nt_init(ptr, NT_PRFPREG, &nt_fpregset, sizeof(nt_fpregset));
361 	ptr = nt_init(ptr, NT_S390_TIMER, &sa->timer, sizeof(sa->timer));
362 	ptr = nt_init(ptr, NT_S390_TODCMP, &sa->todcmp, sizeof(sa->todcmp));
363 	ptr = nt_init(ptr, NT_S390_TODPREG, &sa->todpreg, sizeof(sa->todpreg));
364 	ptr = nt_init(ptr, NT_S390_CTRS, &sa->ctrs, sizeof(sa->ctrs));
365 	ptr = nt_init(ptr, NT_S390_PREFIX, &sa->prefix, sizeof(sa->prefix));
366 	if (MACHINE_HAS_VX) {
367 		ptr = nt_init(ptr, NT_S390_VXRS_HIGH,
368 			      &sa->vxrs_high, sizeof(sa->vxrs_high));
369 		ptr = nt_init(ptr, NT_S390_VXRS_LOW,
370 			      &sa->vxrs_low, sizeof(sa->vxrs_low));
371 	}
372 	return ptr;
373 }
374 
375 /*
376  * Initialize prpsinfo note (new kernel)
377  */
378 static void *nt_prpsinfo(void *ptr)
379 {
380 	struct elf_prpsinfo prpsinfo;
381 
382 	memset(&prpsinfo, 0, sizeof(prpsinfo));
383 	prpsinfo.pr_sname = 'R';
384 	strcpy(prpsinfo.pr_fname, "vmlinux");
385 	return nt_init(ptr, NT_PRPSINFO, &prpsinfo, sizeof(prpsinfo));
386 }
387 
388 /*
389  * Get vmcoreinfo using lowcore->vmcore_info (new kernel)
390  */
391 static void *get_vmcoreinfo_old(unsigned long *size)
392 {
393 	char nt_name[11], *vmcoreinfo;
394 	Elf64_Nhdr note;
395 	void *addr;
396 
397 	if (copy_oldmem_kernel(&addr, &S390_lowcore.vmcore_info, sizeof(addr)))
398 		return NULL;
399 	memset(nt_name, 0, sizeof(nt_name));
400 	if (copy_oldmem_kernel(&note, addr, sizeof(note)))
401 		return NULL;
402 	if (copy_oldmem_kernel(nt_name, addr + sizeof(note),
403 			       sizeof(nt_name) - 1))
404 		return NULL;
405 	if (strcmp(nt_name, "VMCOREINFO") != 0)
406 		return NULL;
407 	vmcoreinfo = kzalloc_panic(note.n_descsz);
408 	if (copy_oldmem_kernel(vmcoreinfo, addr + 24, note.n_descsz))
409 		return NULL;
410 	*size = note.n_descsz;
411 	return vmcoreinfo;
412 }
413 
414 /*
415  * Initialize vmcoreinfo note (new kernel)
416  */
417 static void *nt_vmcoreinfo(void *ptr)
418 {
419 	unsigned long size;
420 	void *vmcoreinfo;
421 
422 	vmcoreinfo = os_info_old_entry(OS_INFO_VMCOREINFO, &size);
423 	if (!vmcoreinfo)
424 		vmcoreinfo = get_vmcoreinfo_old(&size);
425 	if (!vmcoreinfo)
426 		return ptr;
427 	return nt_init_name(ptr, 0, vmcoreinfo, size, "VMCOREINFO");
428 }
429 
430 /*
431  * Initialize ELF header (new kernel)
432  */
433 static void *ehdr_init(Elf64_Ehdr *ehdr, int mem_chunk_cnt)
434 {
435 	memset(ehdr, 0, sizeof(*ehdr));
436 	memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
437 	ehdr->e_ident[EI_CLASS] = ELFCLASS64;
438 	ehdr->e_ident[EI_DATA] = ELFDATA2MSB;
439 	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
440 	memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
441 	ehdr->e_type = ET_CORE;
442 	ehdr->e_machine = EM_S390;
443 	ehdr->e_version = EV_CURRENT;
444 	ehdr->e_phoff = sizeof(Elf64_Ehdr);
445 	ehdr->e_ehsize = sizeof(Elf64_Ehdr);
446 	ehdr->e_phentsize = sizeof(Elf64_Phdr);
447 	ehdr->e_phnum = mem_chunk_cnt + 1;
448 	return ehdr + 1;
449 }
450 
451 /*
452  * Return CPU count for ELF header (new kernel)
453  */
454 static int get_cpu_cnt(void)
455 {
456 	struct save_area *sa;
457 	int cpus = 0;
458 
459 	list_for_each_entry(sa, &dump_save_areas, list)
460 		if (sa->prefix != 0)
461 			cpus++;
462 	return cpus;
463 }
464 
465 /*
466  * Return memory chunk count for ELF header (new kernel)
467  */
468 static int get_mem_chunk_cnt(void)
469 {
470 	int cnt = 0;
471 	u64 idx;
472 
473 	for_each_mem_range(idx, &memblock.physmem, &oldmem_type, NUMA_NO_NODE,
474 			   MEMBLOCK_NONE, NULL, NULL, NULL)
475 		cnt++;
476 	return cnt;
477 }
478 
479 /*
480  * Initialize ELF loads (new kernel)
481  */
482 static void loads_init(Elf64_Phdr *phdr, u64 loads_offset)
483 {
484 	phys_addr_t start, end;
485 	u64 idx;
486 
487 	for_each_mem_range(idx, &memblock.physmem, &oldmem_type, NUMA_NO_NODE,
488 			   MEMBLOCK_NONE, &start, &end, NULL) {
489 		phdr->p_filesz = end - start;
490 		phdr->p_type = PT_LOAD;
491 		phdr->p_offset = start;
492 		phdr->p_vaddr = start;
493 		phdr->p_paddr = start;
494 		phdr->p_memsz = end - start;
495 		phdr->p_flags = PF_R | PF_W | PF_X;
496 		phdr->p_align = PAGE_SIZE;
497 		phdr++;
498 	}
499 }
500 
501 /*
502  * Initialize notes (new kernel)
503  */
504 static void *notes_init(Elf64_Phdr *phdr, void *ptr, u64 notes_offset)
505 {
506 	struct save_area *sa;
507 	void *ptr_start = ptr;
508 	int cpu;
509 
510 	ptr = nt_prpsinfo(ptr);
511 
512 	cpu = 1;
513 	list_for_each_entry(sa, &dump_save_areas, list)
514 		if (sa->prefix != 0)
515 			ptr = fill_cpu_elf_notes(ptr, cpu++, sa);
516 	ptr = nt_vmcoreinfo(ptr);
517 	memset(phdr, 0, sizeof(*phdr));
518 	phdr->p_type = PT_NOTE;
519 	phdr->p_offset = notes_offset;
520 	phdr->p_filesz = (unsigned long) PTR_SUB(ptr, ptr_start);
521 	phdr->p_memsz = phdr->p_filesz;
522 	return ptr;
523 }
524 
525 /*
526  * Create ELF core header (new kernel)
527  */
528 int elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size)
529 {
530 	Elf64_Phdr *phdr_notes, *phdr_loads;
531 	int mem_chunk_cnt;
532 	void *ptr, *hdr;
533 	u32 alloc_size;
534 	u64 hdr_off;
535 
536 	/* If we are not in kdump or zfcpdump mode return */
537 	if (!OLDMEM_BASE && ipl_info.type != IPL_TYPE_FCP_DUMP)
538 		return 0;
539 	/* If we cannot get HSA size for zfcpdump return error */
540 	if (ipl_info.type == IPL_TYPE_FCP_DUMP && !sclp.hsa_size)
541 		return -ENODEV;
542 
543 	/* For kdump, exclude previous crashkernel memory */
544 	if (OLDMEM_BASE) {
545 		oldmem_region.base = OLDMEM_BASE;
546 		oldmem_region.size = OLDMEM_SIZE;
547 		oldmem_type.total_size = OLDMEM_SIZE;
548 	}
549 
550 	mem_chunk_cnt = get_mem_chunk_cnt();
551 
552 	alloc_size = 0x1000 + get_cpu_cnt() * 0x4a0 +
553 		mem_chunk_cnt * sizeof(Elf64_Phdr);
554 	hdr = kzalloc_panic(alloc_size);
555 	/* Init elf header */
556 	ptr = ehdr_init(hdr, mem_chunk_cnt);
557 	/* Init program headers */
558 	phdr_notes = ptr;
559 	ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr));
560 	phdr_loads = ptr;
561 	ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr) * mem_chunk_cnt);
562 	/* Init notes */
563 	hdr_off = PTR_DIFF(ptr, hdr);
564 	ptr = notes_init(phdr_notes, ptr, ((unsigned long) hdr) + hdr_off);
565 	/* Init loads */
566 	hdr_off = PTR_DIFF(ptr, hdr);
567 	loads_init(phdr_loads, hdr_off);
568 	*addr = (unsigned long long) hdr;
569 	*size = (unsigned long long) hdr_off;
570 	BUG_ON(elfcorehdr_size > alloc_size);
571 	return 0;
572 }
573 
574 /*
575  * Free ELF core header (new kernel)
576  */
577 void elfcorehdr_free(unsigned long long addr)
578 {
579 	kfree((void *)(unsigned long)addr);
580 }
581 
582 /*
583  * Read from ELF header
584  */
585 ssize_t elfcorehdr_read(char *buf, size_t count, u64 *ppos)
586 {
587 	void *src = (void *)(unsigned long)*ppos;
588 
589 	memcpy(buf, src, count);
590 	*ppos += count;
591 	return count;
592 }
593 
594 /*
595  * Read from ELF notes data
596  */
597 ssize_t elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos)
598 {
599 	void *src = (void *)(unsigned long)*ppos;
600 
601 	memcpy(buf, src, count);
602 	*ppos += count;
603 	return count;
604 }
605