1 // SPDX-License-Identifier: GPL-2.0
3 * S390 kdump implementation
5 * Copyright IBM Corp. 2011
6 * Author(s): Michael Holzheu <holzheu@linux.vnet.ibm.com>
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>
25 #define PTR_ADD(x, y) (((char *) (x)) + ((unsigned long) (y)))
26 #define PTR_SUB(x, y) (((char *) (x)) - ((unsigned long) (y)))
27 #define PTR_DIFF(x, y) ((unsigned long)(((char *) (x)) - ((unsigned long) (y))))
29 static struct memblock_region oldmem_region;
31 static struct memblock_type oldmem_type = {
35 .regions = &oldmem_region,
40 struct list_head list;
52 __vector128 vxrs_high[16];
55 static LIST_HEAD(dump_save_areas);
58 * Allocate a save area
60 struct save_area * __init save_area_alloc(bool is_boot_cpu)
64 sa = memblock_alloc(sizeof(*sa), 8);
66 panic("Failed to allocate save area\n");
69 list_add(&sa->list, &dump_save_areas);
71 list_add_tail(&sa->list, &dump_save_areas);
76 * Return the address of the save area for the boot CPU
78 struct save_area * __init save_area_boot_cpu(void)
80 return list_first_entry_or_null(&dump_save_areas, struct save_area, list);
84 * Copy CPU registers into the save area
86 void __init save_area_add_regs(struct save_area *sa, void *regs)
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));
104 * Copy vector registers into the save area
106 void __init save_area_add_vxrs(struct save_area *sa, __vector128 *vxrs)
110 /* Copy lower halves of vector registers 0-15 */
111 for (i = 0; i < 16; i++)
112 memcpy(&sa->vxrs_low[i], &vxrs[i].u[2], 8);
113 /* Copy vector registers 16-31 */
114 memcpy(sa->vxrs_high, vxrs + 16, 16 * sizeof(__vector128));
118 * Return physical address for virtual address
120 static inline void *load_real_addr(void *addr)
122 unsigned long real_addr;
129 : "=a" (real_addr) : "a" (addr) : "cc");
130 return (void *)real_addr;
134 * Copy memory of the old, dumped system to a kernel space virtual address
136 int copy_oldmem_kernel(void *dst, unsigned long src, size_t count)
143 if (!oldmem_data.start && src < sclp.hsa_size) {
144 /* Copy from zfcp/nvme dump HSA area */
145 len = min(count, sclp.hsa_size - src);
146 rc = memcpy_hsa_kernel(dst, src, len);
150 /* Check for swapped kdump oldmem areas */
151 if (oldmem_data.start && src - oldmem_data.start < oldmem_data.size) {
152 src -= oldmem_data.start;
153 len = min(count, oldmem_data.size - src);
154 } else if (oldmem_data.start && src < oldmem_data.size) {
155 len = min(count, oldmem_data.size - src);
156 src += oldmem_data.start;
160 if (is_vmalloc_or_module_addr(dst)) {
161 ra = load_real_addr(dst);
162 len = min(PAGE_SIZE - offset_in_page(ra), len);
166 if (memcpy_real(ra, src, len))
177 * Copy memory of the old, dumped system to a user space virtual address
179 static int copy_oldmem_user(void __user *dst, unsigned long src, size_t count)
185 if (!oldmem_data.start && src < sclp.hsa_size) {
186 /* Copy from zfcp/nvme dump HSA area */
187 len = min(count, sclp.hsa_size - src);
188 rc = memcpy_hsa_user(dst, src, len);
192 /* Check for swapped kdump oldmem areas */
193 if (oldmem_data.start && src - oldmem_data.start < oldmem_data.size) {
194 src -= oldmem_data.start;
195 len = min(count, oldmem_data.size - src);
196 } else if (oldmem_data.start && src < oldmem_data.size) {
197 len = min(count, oldmem_data.size - src);
198 src += oldmem_data.start;
202 rc = copy_to_user_real(dst, src, count);
214 * Copy one page from "oldmem"
216 ssize_t copy_oldmem_page(struct iov_iter *iter, unsigned long pfn, size_t csize,
217 unsigned long offset)
222 if (!(iter_is_iovec(iter) || iov_iter_is_kvec(iter)))
224 /* Multi-segment iterators are not supported */
225 if (iter->nr_segs > 1)
229 src = pfn_to_phys(pfn) + offset;
231 /* XXX: pass the iov_iter down to a common function */
232 if (iter_is_iovec(iter))
233 rc = copy_oldmem_user(iter->iov->iov_base, src, csize);
235 rc = copy_oldmem_kernel(iter->kvec->iov_base, src, csize);
238 iov_iter_advance(iter, csize);
243 * Remap "oldmem" for kdump
245 * For the kdump reserved memory this functions performs a swap operation:
246 * [0 - OLDMEM_SIZE] is mapped to [OLDMEM_BASE - OLDMEM_BASE + OLDMEM_SIZE]
248 static int remap_oldmem_pfn_range_kdump(struct vm_area_struct *vma,
249 unsigned long from, unsigned long pfn,
250 unsigned long size, pgprot_t prot)
252 unsigned long size_old;
255 if (pfn < oldmem_data.size >> PAGE_SHIFT) {
256 size_old = min(size, oldmem_data.size - (pfn << PAGE_SHIFT));
257 rc = remap_pfn_range(vma, from,
258 pfn + (oldmem_data.start >> PAGE_SHIFT),
260 if (rc || size == size_old)
264 pfn += size_old >> PAGE_SHIFT;
266 return remap_pfn_range(vma, from, pfn, size, prot);
270 * Remap "oldmem" for zfcp/nvme dump
272 * We only map available memory above HSA size. Memory below HSA size
273 * is read on demand using the copy_oldmem_page() function.
275 static int remap_oldmem_pfn_range_zfcpdump(struct vm_area_struct *vma,
278 unsigned long size, pgprot_t prot)
280 unsigned long hsa_end = sclp.hsa_size;
281 unsigned long size_hsa;
283 if (pfn < hsa_end >> PAGE_SHIFT) {
284 size_hsa = min(size, hsa_end - (pfn << PAGE_SHIFT));
285 if (size == size_hsa)
289 pfn += size_hsa >> PAGE_SHIFT;
291 return remap_pfn_range(vma, from, pfn, size, prot);
295 * Remap "oldmem" for kdump or zfcp/nvme dump
297 int remap_oldmem_pfn_range(struct vm_area_struct *vma, unsigned long from,
298 unsigned long pfn, unsigned long size, pgprot_t prot)
300 if (oldmem_data.start)
301 return remap_oldmem_pfn_range_kdump(vma, from, pfn, size, prot);
303 return remap_oldmem_pfn_range_zfcpdump(vma, from, pfn, size,
307 static const char *nt_name(Elf64_Word type)
309 const char *name = "LINUX";
311 if (type == NT_PRPSINFO || type == NT_PRSTATUS || type == NT_PRFPREG)
312 name = KEXEC_CORE_NOTE_NAME;
317 * Initialize ELF note
319 static void *nt_init_name(void *buf, Elf64_Word type, void *desc, int d_len,
325 note = (Elf64_Nhdr *)buf;
326 note->n_namesz = strlen(name) + 1;
327 note->n_descsz = d_len;
329 len = sizeof(Elf64_Nhdr);
331 memcpy(buf + len, name, note->n_namesz);
332 len = roundup(len + note->n_namesz, 4);
334 memcpy(buf + len, desc, note->n_descsz);
335 len = roundup(len + note->n_descsz, 4);
337 return PTR_ADD(buf, len);
340 static inline void *nt_init(void *buf, Elf64_Word type, void *desc, int d_len)
342 return nt_init_name(buf, type, desc, d_len, nt_name(type));
346 * Calculate the size of ELF note
348 static size_t nt_size_name(int d_len, const char *name)
352 size = sizeof(Elf64_Nhdr);
353 size += roundup(strlen(name) + 1, 4);
354 size += roundup(d_len, 4);
359 static inline size_t nt_size(Elf64_Word type, int d_len)
361 return nt_size_name(d_len, nt_name(type));
365 * Fill ELF notes for one CPU with save area registers
367 static void *fill_cpu_elf_notes(void *ptr, int cpu, struct save_area *sa)
369 struct elf_prstatus nt_prstatus;
370 elf_fpregset_t nt_fpregset;
372 /* Prepare prstatus note */
373 memset(&nt_prstatus, 0, sizeof(nt_prstatus));
374 memcpy(&nt_prstatus.pr_reg.gprs, sa->gprs, sizeof(sa->gprs));
375 memcpy(&nt_prstatus.pr_reg.psw, sa->psw, sizeof(sa->psw));
376 memcpy(&nt_prstatus.pr_reg.acrs, sa->acrs, sizeof(sa->acrs));
377 nt_prstatus.common.pr_pid = cpu;
378 /* Prepare fpregset (floating point) note */
379 memset(&nt_fpregset, 0, sizeof(nt_fpregset));
380 memcpy(&nt_fpregset.fpc, &sa->fpc, sizeof(sa->fpc));
381 memcpy(&nt_fpregset.fprs, &sa->fprs, sizeof(sa->fprs));
382 /* Create ELF notes for the CPU */
383 ptr = nt_init(ptr, NT_PRSTATUS, &nt_prstatus, sizeof(nt_prstatus));
384 ptr = nt_init(ptr, NT_PRFPREG, &nt_fpregset, sizeof(nt_fpregset));
385 ptr = nt_init(ptr, NT_S390_TIMER, &sa->timer, sizeof(sa->timer));
386 ptr = nt_init(ptr, NT_S390_TODCMP, &sa->todcmp, sizeof(sa->todcmp));
387 ptr = nt_init(ptr, NT_S390_TODPREG, &sa->todpreg, sizeof(sa->todpreg));
388 ptr = nt_init(ptr, NT_S390_CTRS, &sa->ctrs, sizeof(sa->ctrs));
389 ptr = nt_init(ptr, NT_S390_PREFIX, &sa->prefix, sizeof(sa->prefix));
390 if (MACHINE_HAS_VX) {
391 ptr = nt_init(ptr, NT_S390_VXRS_HIGH,
392 &sa->vxrs_high, sizeof(sa->vxrs_high));
393 ptr = nt_init(ptr, NT_S390_VXRS_LOW,
394 &sa->vxrs_low, sizeof(sa->vxrs_low));
400 * Calculate size of ELF notes per cpu
402 static size_t get_cpu_elf_notes_size(void)
404 struct save_area *sa = NULL;
407 size = nt_size(NT_PRSTATUS, sizeof(struct elf_prstatus));
408 size += nt_size(NT_PRFPREG, sizeof(elf_fpregset_t));
409 size += nt_size(NT_S390_TIMER, sizeof(sa->timer));
410 size += nt_size(NT_S390_TODCMP, sizeof(sa->todcmp));
411 size += nt_size(NT_S390_TODPREG, sizeof(sa->todpreg));
412 size += nt_size(NT_S390_CTRS, sizeof(sa->ctrs));
413 size += nt_size(NT_S390_PREFIX, sizeof(sa->prefix));
414 if (MACHINE_HAS_VX) {
415 size += nt_size(NT_S390_VXRS_HIGH, sizeof(sa->vxrs_high));
416 size += nt_size(NT_S390_VXRS_LOW, sizeof(sa->vxrs_low));
423 * Initialize prpsinfo note (new kernel)
425 static void *nt_prpsinfo(void *ptr)
427 struct elf_prpsinfo prpsinfo;
429 memset(&prpsinfo, 0, sizeof(prpsinfo));
430 prpsinfo.pr_sname = 'R';
431 strcpy(prpsinfo.pr_fname, "vmlinux");
432 return nt_init(ptr, NT_PRPSINFO, &prpsinfo, sizeof(prpsinfo));
436 * Get vmcoreinfo using lowcore->vmcore_info (new kernel)
438 static void *get_vmcoreinfo_old(unsigned long *size)
440 char nt_name[11], *vmcoreinfo;
444 if (copy_oldmem_kernel(&addr, __LC_VMCORE_INFO, sizeof(addr)))
446 memset(nt_name, 0, sizeof(nt_name));
447 if (copy_oldmem_kernel(¬e, addr, sizeof(note)))
449 if (copy_oldmem_kernel(nt_name, addr + sizeof(note),
450 sizeof(nt_name) - 1))
452 if (strcmp(nt_name, VMCOREINFO_NOTE_NAME) != 0)
454 vmcoreinfo = kzalloc(note.n_descsz, GFP_KERNEL);
457 if (copy_oldmem_kernel(vmcoreinfo, addr + 24, note.n_descsz)) {
461 *size = note.n_descsz;
466 * Initialize vmcoreinfo note (new kernel)
468 static void *nt_vmcoreinfo(void *ptr)
470 const char *name = VMCOREINFO_NOTE_NAME;
474 vmcoreinfo = os_info_old_entry(OS_INFO_VMCOREINFO, &size);
476 return nt_init_name(ptr, 0, vmcoreinfo, size, name);
478 vmcoreinfo = get_vmcoreinfo_old(&size);
481 ptr = nt_init_name(ptr, 0, vmcoreinfo, size, name);
486 static size_t nt_vmcoreinfo_size(void)
488 const char *name = VMCOREINFO_NOTE_NAME;
492 vmcoreinfo = os_info_old_entry(OS_INFO_VMCOREINFO, &size);
494 return nt_size_name(size, name);
496 vmcoreinfo = get_vmcoreinfo_old(&size);
501 return nt_size_name(size, name);
505 * Initialize final note (needed for /proc/vmcore code)
507 static void *nt_final(void *ptr)
511 note = (Elf64_Nhdr *) ptr;
515 return PTR_ADD(ptr, sizeof(Elf64_Nhdr));
519 * Initialize ELF header (new kernel)
521 static void *ehdr_init(Elf64_Ehdr *ehdr, int mem_chunk_cnt)
523 memset(ehdr, 0, sizeof(*ehdr));
524 memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
525 ehdr->e_ident[EI_CLASS] = ELFCLASS64;
526 ehdr->e_ident[EI_DATA] = ELFDATA2MSB;
527 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
528 memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
529 ehdr->e_type = ET_CORE;
530 ehdr->e_machine = EM_S390;
531 ehdr->e_version = EV_CURRENT;
532 ehdr->e_phoff = sizeof(Elf64_Ehdr);
533 ehdr->e_ehsize = sizeof(Elf64_Ehdr);
534 ehdr->e_phentsize = sizeof(Elf64_Phdr);
535 ehdr->e_phnum = mem_chunk_cnt + 1;
540 * Return CPU count for ELF header (new kernel)
542 static int get_cpu_cnt(void)
544 struct save_area *sa;
547 list_for_each_entry(sa, &dump_save_areas, list)
554 * Return memory chunk count for ELF header (new kernel)
556 static int get_mem_chunk_cnt(void)
561 for_each_physmem_range(idx, &oldmem_type, NULL, NULL)
567 * Initialize ELF loads (new kernel)
569 static void loads_init(Elf64_Phdr *phdr, u64 loads_offset)
571 phys_addr_t start, end;
574 for_each_physmem_range(idx, &oldmem_type, &start, &end) {
575 phdr->p_filesz = end - start;
576 phdr->p_type = PT_LOAD;
577 phdr->p_offset = start;
578 phdr->p_vaddr = start;
579 phdr->p_paddr = start;
580 phdr->p_memsz = end - start;
581 phdr->p_flags = PF_R | PF_W | PF_X;
582 phdr->p_align = PAGE_SIZE;
588 * Initialize notes (new kernel)
590 static void *notes_init(Elf64_Phdr *phdr, void *ptr, u64 notes_offset)
592 struct save_area *sa;
593 void *ptr_start = ptr;
596 ptr = nt_prpsinfo(ptr);
599 list_for_each_entry(sa, &dump_save_areas, list)
601 ptr = fill_cpu_elf_notes(ptr, cpu++, sa);
602 ptr = nt_vmcoreinfo(ptr);
604 memset(phdr, 0, sizeof(*phdr));
605 phdr->p_type = PT_NOTE;
606 phdr->p_offset = notes_offset;
607 phdr->p_filesz = (unsigned long) PTR_SUB(ptr, ptr_start);
608 phdr->p_memsz = phdr->p_filesz;
612 static size_t get_elfcorehdr_size(int mem_chunk_cnt)
616 size = sizeof(Elf64_Ehdr);
618 size += sizeof(Elf64_Phdr);
620 size += nt_size(NT_PRPSINFO, sizeof(struct elf_prpsinfo));
622 size += get_cpu_cnt() * get_cpu_elf_notes_size();
624 size += nt_vmcoreinfo_size();
626 size += sizeof(Elf64_Nhdr);
628 size += mem_chunk_cnt * sizeof(Elf64_Phdr);
634 * Create ELF core header (new kernel)
636 int elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size)
638 Elf64_Phdr *phdr_notes, *phdr_loads;
644 /* If we are not in kdump or zfcp/nvme dump mode return */
645 if (!oldmem_data.start && !is_ipl_type_dump())
647 /* If we cannot get HSA size for zfcp/nvme dump return error */
648 if (is_ipl_type_dump() && !sclp.hsa_size)
651 /* For kdump, exclude previous crashkernel memory */
652 if (oldmem_data.start) {
653 oldmem_region.base = oldmem_data.start;
654 oldmem_region.size = oldmem_data.size;
655 oldmem_type.total_size = oldmem_data.size;
658 mem_chunk_cnt = get_mem_chunk_cnt();
660 alloc_size = get_elfcorehdr_size(mem_chunk_cnt);
662 hdr = kzalloc(alloc_size, GFP_KERNEL);
664 /* Without elfcorehdr /proc/vmcore cannot be created. Thus creating
665 * a dump with this crash kernel will fail. Panic now to allow other
666 * dump mechanisms to take over.
669 panic("s390 kdump allocating elfcorehdr failed");
671 /* Init elf header */
672 ptr = ehdr_init(hdr, mem_chunk_cnt);
673 /* Init program headers */
675 ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr));
677 ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr) * mem_chunk_cnt);
679 hdr_off = PTR_DIFF(ptr, hdr);
680 ptr = notes_init(phdr_notes, ptr, ((unsigned long) hdr) + hdr_off);
682 hdr_off = PTR_DIFF(ptr, hdr);
683 loads_init(phdr_loads, hdr_off);
684 *addr = (unsigned long long) hdr;
685 *size = (unsigned long long) hdr_off;
686 BUG_ON(elfcorehdr_size > alloc_size);
691 * Free ELF core header (new kernel)
693 void elfcorehdr_free(unsigned long long addr)
695 kfree((void *)(unsigned long)addr);
699 * Read from ELF header
701 ssize_t elfcorehdr_read(char *buf, size_t count, u64 *ppos)
703 void *src = (void *)(unsigned long)*ppos;
705 memcpy(buf, src, count);
711 * Read from ELF notes data
713 ssize_t elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos)
715 void *src = (void *)(unsigned long)*ppos;
717 memcpy(buf, src, count);