1 // SPDX-License-Identifier: GPL-2.0
3 * S390 kdump implementation
5 * Copyright IBM Corp. 2011
9 #include <linux/crash_dump.h>
10 #include <asm/lowcore.h>
11 #include <linux/kernel.h>
12 #include <linux/init.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>
24 #include <asm/maccess.h>
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))))
31 static struct memblock_region oldmem_region;
33 static struct memblock_type oldmem_type = {
37 .regions = &oldmem_region,
42 struct list_head list;
54 __vector128 vxrs_high[16];
57 static LIST_HEAD(dump_save_areas);
60 * Allocate a save area
62 struct save_area * __init save_area_alloc(bool is_boot_cpu)
66 sa = memblock_alloc(sizeof(*sa), 8);
71 list_add(&sa->list, &dump_save_areas);
73 list_add_tail(&sa->list, &dump_save_areas);
78 * Return the address of the save area for the boot CPU
80 struct save_area * __init save_area_boot_cpu(void)
82 return list_first_entry_or_null(&dump_save_areas, struct save_area, list);
86 * Copy CPU registers into the save area
88 void __init save_area_add_regs(struct save_area *sa, void *regs)
92 lc = (struct lowcore *)(regs - __LC_FPREGS_SAVE_AREA);
93 memcpy(&sa->psw, &lc->psw_save_area, sizeof(sa->psw));
94 memcpy(&sa->ctrs, &lc->cregs_save_area, sizeof(sa->ctrs));
95 memcpy(&sa->gprs, &lc->gpregs_save_area, sizeof(sa->gprs));
96 memcpy(&sa->acrs, &lc->access_regs_save_area, sizeof(sa->acrs));
97 memcpy(&sa->fprs, &lc->floating_pt_save_area, sizeof(sa->fprs));
98 memcpy(&sa->fpc, &lc->fpt_creg_save_area, sizeof(sa->fpc));
99 memcpy(&sa->prefix, &lc->prefixreg_save_area, sizeof(sa->prefix));
100 memcpy(&sa->todpreg, &lc->tod_progreg_save_area, sizeof(sa->todpreg));
101 memcpy(&sa->timer, &lc->cpu_timer_save_area, sizeof(sa->timer));
102 memcpy(&sa->todcmp, &lc->clock_comp_save_area, sizeof(sa->todcmp));
106 * Copy vector registers into the save area
108 void __init save_area_add_vxrs(struct save_area *sa, __vector128 *vxrs)
112 /* Copy lower halves of vector registers 0-15 */
113 for (i = 0; i < 16; i++)
114 sa->vxrs_low[i] = vxrs[i].low;
115 /* Copy vector registers 16-31 */
116 memcpy(sa->vxrs_high, vxrs + 16, 16 * sizeof(__vector128));
119 static size_t copy_oldmem_iter(struct iov_iter *iter, unsigned long src, size_t count)
121 size_t len, copied, res = 0;
124 if (!oldmem_data.start && src < sclp.hsa_size) {
125 /* Copy from zfcp/nvme dump HSA area */
126 len = min(count, sclp.hsa_size - src);
127 copied = memcpy_hsa_iter(iter, src, len);
129 /* Check for swapped kdump oldmem areas */
130 if (oldmem_data.start && src - oldmem_data.start < oldmem_data.size) {
131 src -= oldmem_data.start;
132 len = min(count, oldmem_data.size - src);
133 } else if (oldmem_data.start && src < oldmem_data.size) {
134 len = min(count, oldmem_data.size - src);
135 src += oldmem_data.start;
139 copied = memcpy_real_iter(iter, src, len);
150 int copy_oldmem_kernel(void *dst, unsigned long src, size_t count)
152 struct iov_iter iter;
156 kvec.iov_len = count;
157 iov_iter_kvec(&iter, ITER_DEST, &kvec, 1, count);
158 if (copy_oldmem_iter(&iter, src, count) < count)
164 * Copy one page from "oldmem"
166 ssize_t copy_oldmem_page(struct iov_iter *iter, unsigned long pfn, size_t csize,
167 unsigned long offset)
171 src = pfn_to_phys(pfn) + offset;
172 return copy_oldmem_iter(iter, src, csize);
176 * Remap "oldmem" for kdump
178 * For the kdump reserved memory this functions performs a swap operation:
179 * [0 - OLDMEM_SIZE] is mapped to [OLDMEM_BASE - OLDMEM_BASE + OLDMEM_SIZE]
181 static int remap_oldmem_pfn_range_kdump(struct vm_area_struct *vma,
182 unsigned long from, unsigned long pfn,
183 unsigned long size, pgprot_t prot)
185 unsigned long size_old;
188 if (pfn < oldmem_data.size >> PAGE_SHIFT) {
189 size_old = min(size, oldmem_data.size - (pfn << PAGE_SHIFT));
190 rc = remap_pfn_range(vma, from,
191 pfn + (oldmem_data.start >> PAGE_SHIFT),
193 if (rc || size == size_old)
197 pfn += size_old >> PAGE_SHIFT;
199 return remap_pfn_range(vma, from, pfn, size, prot);
203 * Remap "oldmem" for zfcp/nvme dump
205 * We only map available memory above HSA size. Memory below HSA size
206 * is read on demand using the copy_oldmem_page() function.
208 static int remap_oldmem_pfn_range_zfcpdump(struct vm_area_struct *vma,
211 unsigned long size, pgprot_t prot)
213 unsigned long hsa_end = sclp.hsa_size;
214 unsigned long size_hsa;
216 if (pfn < hsa_end >> PAGE_SHIFT) {
217 size_hsa = min(size, hsa_end - (pfn << PAGE_SHIFT));
218 if (size == size_hsa)
222 pfn += size_hsa >> PAGE_SHIFT;
224 return remap_pfn_range(vma, from, pfn, size, prot);
228 * Remap "oldmem" for kdump or zfcp/nvme dump
230 int remap_oldmem_pfn_range(struct vm_area_struct *vma, unsigned long from,
231 unsigned long pfn, unsigned long size, pgprot_t prot)
233 if (oldmem_data.start)
234 return remap_oldmem_pfn_range_kdump(vma, from, pfn, size, prot);
236 return remap_oldmem_pfn_range_zfcpdump(vma, from, pfn, size,
240 static const char *nt_name(Elf64_Word type)
242 const char *name = "LINUX";
244 if (type == NT_PRPSINFO || type == NT_PRSTATUS || type == NT_PRFPREG)
245 name = KEXEC_CORE_NOTE_NAME;
250 * Initialize ELF note
252 static void *nt_init_name(void *buf, Elf64_Word type, void *desc, int d_len,
258 note = (Elf64_Nhdr *)buf;
259 note->n_namesz = strlen(name) + 1;
260 note->n_descsz = d_len;
262 len = sizeof(Elf64_Nhdr);
264 memcpy(buf + len, name, note->n_namesz);
265 len = roundup(len + note->n_namesz, 4);
267 memcpy(buf + len, desc, note->n_descsz);
268 len = roundup(len + note->n_descsz, 4);
270 return PTR_ADD(buf, len);
273 static inline void *nt_init(void *buf, Elf64_Word type, void *desc, int d_len)
275 return nt_init_name(buf, type, desc, d_len, nt_name(type));
279 * Calculate the size of ELF note
281 static size_t nt_size_name(int d_len, const char *name)
285 size = sizeof(Elf64_Nhdr);
286 size += roundup(strlen(name) + 1, 4);
287 size += roundup(d_len, 4);
292 static inline size_t nt_size(Elf64_Word type, int d_len)
294 return nt_size_name(d_len, nt_name(type));
298 * Fill ELF notes for one CPU with save area registers
300 static void *fill_cpu_elf_notes(void *ptr, int cpu, struct save_area *sa)
302 struct elf_prstatus nt_prstatus;
303 elf_fpregset_t nt_fpregset;
305 /* Prepare prstatus note */
306 memset(&nt_prstatus, 0, sizeof(nt_prstatus));
307 memcpy(&nt_prstatus.pr_reg.gprs, sa->gprs, sizeof(sa->gprs));
308 memcpy(&nt_prstatus.pr_reg.psw, sa->psw, sizeof(sa->psw));
309 memcpy(&nt_prstatus.pr_reg.acrs, sa->acrs, sizeof(sa->acrs));
310 nt_prstatus.common.pr_pid = cpu;
311 /* Prepare fpregset (floating point) note */
312 memset(&nt_fpregset, 0, sizeof(nt_fpregset));
313 memcpy(&nt_fpregset.fpc, &sa->fpc, sizeof(sa->fpc));
314 memcpy(&nt_fpregset.fprs, &sa->fprs, sizeof(sa->fprs));
315 /* Create ELF notes for the CPU */
316 ptr = nt_init(ptr, NT_PRSTATUS, &nt_prstatus, sizeof(nt_prstatus));
317 ptr = nt_init(ptr, NT_PRFPREG, &nt_fpregset, sizeof(nt_fpregset));
318 ptr = nt_init(ptr, NT_S390_TIMER, &sa->timer, sizeof(sa->timer));
319 ptr = nt_init(ptr, NT_S390_TODCMP, &sa->todcmp, sizeof(sa->todcmp));
320 ptr = nt_init(ptr, NT_S390_TODPREG, &sa->todpreg, sizeof(sa->todpreg));
321 ptr = nt_init(ptr, NT_S390_CTRS, &sa->ctrs, sizeof(sa->ctrs));
322 ptr = nt_init(ptr, NT_S390_PREFIX, &sa->prefix, sizeof(sa->prefix));
324 ptr = nt_init(ptr, NT_S390_VXRS_HIGH,
325 &sa->vxrs_high, sizeof(sa->vxrs_high));
326 ptr = nt_init(ptr, NT_S390_VXRS_LOW,
327 &sa->vxrs_low, sizeof(sa->vxrs_low));
333 * Calculate size of ELF notes per cpu
335 static size_t get_cpu_elf_notes_size(void)
337 struct save_area *sa = NULL;
340 size = nt_size(NT_PRSTATUS, sizeof(struct elf_prstatus));
341 size += nt_size(NT_PRFPREG, sizeof(elf_fpregset_t));
342 size += nt_size(NT_S390_TIMER, sizeof(sa->timer));
343 size += nt_size(NT_S390_TODCMP, sizeof(sa->todcmp));
344 size += nt_size(NT_S390_TODPREG, sizeof(sa->todpreg));
345 size += nt_size(NT_S390_CTRS, sizeof(sa->ctrs));
346 size += nt_size(NT_S390_PREFIX, sizeof(sa->prefix));
348 size += nt_size(NT_S390_VXRS_HIGH, sizeof(sa->vxrs_high));
349 size += nt_size(NT_S390_VXRS_LOW, sizeof(sa->vxrs_low));
356 * Initialize prpsinfo note (new kernel)
358 static void *nt_prpsinfo(void *ptr)
360 struct elf_prpsinfo prpsinfo;
362 memset(&prpsinfo, 0, sizeof(prpsinfo));
363 prpsinfo.pr_sname = 'R';
364 strcpy(prpsinfo.pr_fname, "vmlinux");
365 return nt_init(ptr, NT_PRPSINFO, &prpsinfo, sizeof(prpsinfo));
369 * Get vmcoreinfo using lowcore->vmcore_info (new kernel)
371 static void *get_vmcoreinfo_old(unsigned long *size)
373 char nt_name[11], *vmcoreinfo;
377 if (copy_oldmem_kernel(&addr, __LC_VMCORE_INFO, sizeof(addr)))
379 memset(nt_name, 0, sizeof(nt_name));
380 if (copy_oldmem_kernel(¬e, addr, sizeof(note)))
382 if (copy_oldmem_kernel(nt_name, addr + sizeof(note),
383 sizeof(nt_name) - 1))
385 if (strcmp(nt_name, VMCOREINFO_NOTE_NAME) != 0)
387 vmcoreinfo = kzalloc(note.n_descsz, GFP_KERNEL);
390 if (copy_oldmem_kernel(vmcoreinfo, addr + 24, note.n_descsz)) {
394 *size = note.n_descsz;
399 * Initialize vmcoreinfo note (new kernel)
401 static void *nt_vmcoreinfo(void *ptr)
403 const char *name = VMCOREINFO_NOTE_NAME;
407 vmcoreinfo = os_info_old_entry(OS_INFO_VMCOREINFO, &size);
409 return nt_init_name(ptr, 0, vmcoreinfo, size, name);
411 vmcoreinfo = get_vmcoreinfo_old(&size);
414 ptr = nt_init_name(ptr, 0, vmcoreinfo, size, name);
419 static size_t nt_vmcoreinfo_size(void)
421 const char *name = VMCOREINFO_NOTE_NAME;
425 vmcoreinfo = os_info_old_entry(OS_INFO_VMCOREINFO, &size);
427 return nt_size_name(size, name);
429 vmcoreinfo = get_vmcoreinfo_old(&size);
434 return nt_size_name(size, name);
438 * Initialize final note (needed for /proc/vmcore code)
440 static void *nt_final(void *ptr)
444 note = (Elf64_Nhdr *) ptr;
448 return PTR_ADD(ptr, sizeof(Elf64_Nhdr));
452 * Initialize ELF header (new kernel)
454 static void *ehdr_init(Elf64_Ehdr *ehdr, int mem_chunk_cnt)
456 memset(ehdr, 0, sizeof(*ehdr));
457 memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
458 ehdr->e_ident[EI_CLASS] = ELFCLASS64;
459 ehdr->e_ident[EI_DATA] = ELFDATA2MSB;
460 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
461 memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
462 ehdr->e_type = ET_CORE;
463 ehdr->e_machine = EM_S390;
464 ehdr->e_version = EV_CURRENT;
465 ehdr->e_phoff = sizeof(Elf64_Ehdr);
466 ehdr->e_ehsize = sizeof(Elf64_Ehdr);
467 ehdr->e_phentsize = sizeof(Elf64_Phdr);
469 * Number of memory chunk PT_LOAD program headers plus one kernel
470 * image PT_LOAD program header plus one PT_NOTE program header.
472 ehdr->e_phnum = mem_chunk_cnt + 1 + 1;
477 * Return CPU count for ELF header (new kernel)
479 static int get_cpu_cnt(void)
481 struct save_area *sa;
484 list_for_each_entry(sa, &dump_save_areas, list)
491 * Return memory chunk count for ELF header (new kernel)
493 static int get_mem_chunk_cnt(void)
498 for_each_physmem_range(idx, &oldmem_type, NULL, NULL)
504 * Initialize ELF loads (new kernel)
506 static void loads_init(Elf64_Phdr *phdr)
508 unsigned long old_identity_base = os_info_old_value(OS_INFO_IDENTITY_BASE);
509 phys_addr_t start, end;
512 for_each_physmem_range(idx, &oldmem_type, &start, &end) {
513 phdr->p_type = PT_LOAD;
514 phdr->p_vaddr = old_identity_base + start;
515 phdr->p_offset = start;
516 phdr->p_paddr = start;
517 phdr->p_filesz = end - start;
518 phdr->p_memsz = end - start;
519 phdr->p_flags = PF_R | PF_W | PF_X;
520 phdr->p_align = PAGE_SIZE;
526 * Prepare PT_LOAD type program header for kernel image region
528 static void text_init(Elf64_Phdr *phdr)
530 unsigned long start_phys = os_info_old_value(OS_INFO_IMAGE_PHYS);
531 unsigned long start = os_info_old_value(OS_INFO_IMAGE_START);
532 unsigned long end = os_info_old_value(OS_INFO_IMAGE_END);
534 phdr->p_type = PT_LOAD;
535 phdr->p_vaddr = start;
536 phdr->p_filesz = end - start;
537 phdr->p_memsz = end - start;
538 phdr->p_offset = start_phys;
539 phdr->p_paddr = start_phys;
540 phdr->p_flags = PF_R | PF_W | PF_X;
541 phdr->p_align = PAGE_SIZE;
545 * Initialize notes (new kernel)
547 static void *notes_init(Elf64_Phdr *phdr, void *ptr, u64 notes_offset)
549 struct save_area *sa;
550 void *ptr_start = ptr;
553 ptr = nt_prpsinfo(ptr);
556 list_for_each_entry(sa, &dump_save_areas, list)
558 ptr = fill_cpu_elf_notes(ptr, cpu++, sa);
559 ptr = nt_vmcoreinfo(ptr);
561 memset(phdr, 0, sizeof(*phdr));
562 phdr->p_type = PT_NOTE;
563 phdr->p_offset = notes_offset;
564 phdr->p_filesz = (unsigned long) PTR_SUB(ptr, ptr_start);
565 phdr->p_memsz = phdr->p_filesz;
569 static size_t get_elfcorehdr_size(int mem_chunk_cnt)
573 size = sizeof(Elf64_Ehdr);
575 size += sizeof(Elf64_Phdr);
577 size += nt_size(NT_PRPSINFO, sizeof(struct elf_prpsinfo));
579 size += get_cpu_cnt() * get_cpu_elf_notes_size();
581 size += nt_vmcoreinfo_size();
583 size += sizeof(Elf64_Nhdr);
584 /* PT_LOAD type program header for kernel text region */
585 size += sizeof(Elf64_Phdr);
587 size += mem_chunk_cnt * sizeof(Elf64_Phdr);
593 * Create ELF core header (new kernel)
595 int elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size)
597 Elf64_Phdr *phdr_notes, *phdr_loads, *phdr_text;
603 /* If we are not in kdump or zfcp/nvme dump mode return */
604 if (!oldmem_data.start && !is_ipl_type_dump())
606 /* If we cannot get HSA size for zfcp/nvme dump return error */
607 if (is_ipl_type_dump() && !sclp.hsa_size)
610 /* For kdump, exclude previous crashkernel memory */
611 if (oldmem_data.start) {
612 oldmem_region.base = oldmem_data.start;
613 oldmem_region.size = oldmem_data.size;
614 oldmem_type.total_size = oldmem_data.size;
617 mem_chunk_cnt = get_mem_chunk_cnt();
619 alloc_size = get_elfcorehdr_size(mem_chunk_cnt);
621 hdr = kzalloc(alloc_size, GFP_KERNEL);
623 /* Without elfcorehdr /proc/vmcore cannot be created. Thus creating
624 * a dump with this crash kernel will fail. Panic now to allow other
625 * dump mechanisms to take over.
628 panic("s390 kdump allocating elfcorehdr failed");
630 /* Init elf header */
631 ptr = ehdr_init(hdr, mem_chunk_cnt);
632 /* Init program headers */
634 ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr));
636 ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr));
638 ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr) * mem_chunk_cnt);
640 hdr_off = PTR_DIFF(ptr, hdr);
641 ptr = notes_init(phdr_notes, ptr, ((unsigned long) hdr) + hdr_off);
642 /* Init kernel text program header */
643 text_init(phdr_text);
645 loads_init(phdr_loads);
646 /* Finalize program headers */
647 hdr_off = PTR_DIFF(ptr, hdr);
648 *addr = (unsigned long long) hdr;
649 *size = (unsigned long long) hdr_off;
650 BUG_ON(elfcorehdr_size > alloc_size);
655 * Free ELF core header (new kernel)
657 void elfcorehdr_free(unsigned long long addr)
659 kfree((void *)(unsigned long)addr);
663 * Read from ELF header
665 ssize_t elfcorehdr_read(char *buf, size_t count, u64 *ppos)
667 void *src = (void *)(unsigned long)*ppos;
669 memcpy(buf, src, count);
675 * Read from ELF notes data
677 ssize_t elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos)
679 void *src = (void *)(unsigned long)*ppos;
681 memcpy(buf, src, count);