2 * S390 kdump implementation
4 * Copyright IBM Corp. 2011
5 * Author(s): Michael Holzheu <holzheu@linux.vnet.ibm.com>
8 #include <linux/crash_dump.h>
9 #include <asm/lowcore.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/gfp.h>
13 #include <linux/slab.h>
14 #include <linux/bootmem.h>
15 #include <linux/elf.h>
16 #include <asm/os_info.h>
21 #define PTR_ADD(x, y) (((char *) (x)) + ((unsigned long) (y)))
22 #define PTR_SUB(x, y) (((char *) (x)) - ((unsigned long) (y)))
23 #define PTR_DIFF(x, y) ((unsigned long)(((char *) (x)) - ((unsigned long) (y))))
25 struct dump_save_areas dump_save_areas;
28 * Allocate and add a save area for a CPU
30 struct save_area *dump_save_area_create(int cpu)
32 struct save_area **save_areas, *save_area;
34 save_area = kmalloc(sizeof(*save_area), GFP_KERNEL);
37 if (cpu + 1 > dump_save_areas.count) {
38 dump_save_areas.count = cpu + 1;
39 save_areas = krealloc(dump_save_areas.areas,
40 dump_save_areas.count * sizeof(void *),
41 GFP_KERNEL | __GFP_ZERO);
46 dump_save_areas.areas = save_areas;
48 dump_save_areas.areas[cpu] = save_area;
53 * Return physical address for virtual address
55 static inline void *load_real_addr(void *addr)
57 unsigned long real_addr;
64 : "=a" (real_addr) : "a" (addr) : "cc");
65 return (void *)real_addr;
69 * Copy real to virtual or real memory
71 static int copy_from_realmem(void *dest, void *src, size_t count)
77 if (!is_vmalloc_or_module_addr(dest))
78 return memcpy_real(dest, src, count);
80 size = min(count, PAGE_SIZE - (__pa(dest) & ~PAGE_MASK));
81 if (memcpy_real(load_real_addr(dest), src, size))
91 * Pointer to ELF header in new kernel
93 static void *elfcorehdr_newmem;
96 * Copy one page from zfcpdump "oldmem"
98 * For pages below HSA size memory from the HSA is copied. Otherwise
99 * real memory copy is used.
101 static ssize_t copy_oldmem_page_zfcpdump(char *buf, size_t csize,
102 unsigned long src, int userbuf)
106 if (src < sclp_get_hsa_size()) {
107 rc = memcpy_hsa(buf, src, csize, userbuf);
110 rc = copy_to_user_real((void __force __user *) buf,
111 (void *) src, csize);
113 rc = memcpy_real(buf, (void *) src, csize);
115 return rc ? rc : csize;
119 * Copy one page from kdump "oldmem"
121 * For the kdump reserved memory this functions performs a swap operation:
122 * - [OLDMEM_BASE - OLDMEM_BASE + OLDMEM_SIZE] is mapped to [0 - OLDMEM_SIZE].
123 * - [0 - OLDMEM_SIZE] is mapped to [OLDMEM_BASE - OLDMEM_BASE + OLDMEM_SIZE]
125 static ssize_t copy_oldmem_page_kdump(char *buf, size_t csize,
126 unsigned long src, int userbuf)
131 if (src < OLDMEM_SIZE)
133 else if (src > OLDMEM_BASE &&
134 src < OLDMEM_BASE + OLDMEM_SIZE)
137 rc = copy_to_user_real((void __force __user *) buf,
138 (void *) src, csize);
140 rc = copy_from_realmem(buf, (void *) src, csize);
141 return (rc == 0) ? rc : csize;
145 * Copy one page from "oldmem"
147 ssize_t copy_oldmem_page(unsigned long pfn, char *buf, size_t csize,
148 unsigned long offset, int userbuf)
154 src = (pfn << PAGE_SHIFT) + offset;
156 return copy_oldmem_page_kdump(buf, csize, src, userbuf);
158 return copy_oldmem_page_zfcpdump(buf, csize, src, userbuf);
162 * Remap "oldmem" for kdump
164 * For the kdump reserved memory this functions performs a swap operation:
165 * [0 - OLDMEM_SIZE] is mapped to [OLDMEM_BASE - OLDMEM_BASE + OLDMEM_SIZE]
167 static int remap_oldmem_pfn_range_kdump(struct vm_area_struct *vma,
168 unsigned long from, unsigned long pfn,
169 unsigned long size, pgprot_t prot)
171 unsigned long size_old;
174 if (pfn < OLDMEM_SIZE >> PAGE_SHIFT) {
175 size_old = min(size, OLDMEM_SIZE - (pfn << PAGE_SHIFT));
176 rc = remap_pfn_range(vma, from,
177 pfn + (OLDMEM_BASE >> PAGE_SHIFT),
179 if (rc || size == size_old)
183 pfn += size_old >> PAGE_SHIFT;
185 return remap_pfn_range(vma, from, pfn, size, prot);
189 * Remap "oldmem" for zfcpdump
191 * We only map available memory above HSA size. Memory below HSA size
192 * is read on demand using the copy_oldmem_page() function.
194 static int remap_oldmem_pfn_range_zfcpdump(struct vm_area_struct *vma,
197 unsigned long size, pgprot_t prot)
199 unsigned long hsa_end = sclp_get_hsa_size();
200 unsigned long size_hsa;
202 if (pfn < hsa_end >> PAGE_SHIFT) {
203 size_hsa = min(size, hsa_end - (pfn << PAGE_SHIFT));
204 if (size == size_hsa)
208 pfn += size_hsa >> PAGE_SHIFT;
210 return remap_pfn_range(vma, from, pfn, size, prot);
214 * Remap "oldmem" for kdump or zfcpdump
216 int remap_oldmem_pfn_range(struct vm_area_struct *vma, unsigned long from,
217 unsigned long pfn, unsigned long size, pgprot_t prot)
220 return remap_oldmem_pfn_range_kdump(vma, from, pfn, size, prot);
222 return remap_oldmem_pfn_range_zfcpdump(vma, from, pfn, size,
227 * Copy memory from old kernel
229 int copy_from_oldmem(void *dest, void *src, size_t count)
231 unsigned long copied = 0;
235 if ((unsigned long) src < OLDMEM_SIZE) {
236 copied = min(count, OLDMEM_SIZE - (unsigned long) src);
237 rc = copy_from_realmem(dest, src + OLDMEM_BASE, copied);
242 unsigned long hsa_end = sclp_get_hsa_size();
243 if ((unsigned long) src < hsa_end) {
244 copied = min(count, hsa_end - (unsigned long) src);
245 rc = memcpy_hsa(dest, (unsigned long) src, copied, 0);
250 return copy_from_realmem(dest + copied, src + copied, count - copied);
254 * Alloc memory and panic in case of ENOMEM
256 static void *kzalloc_panic(int len)
260 rc = kzalloc(len, GFP_KERNEL);
262 panic("s390 kdump kzalloc (%d) failed", len);
267 * Get memory layout and create hole for oldmem
269 static struct mem_chunk *get_memory_layout(void)
271 struct mem_chunk *chunk_array;
273 chunk_array = kzalloc_panic(MEMORY_CHUNKS * sizeof(struct mem_chunk));
274 detect_memory_layout(chunk_array, 0);
275 create_mem_hole(chunk_array, OLDMEM_BASE, OLDMEM_SIZE);
280 * Initialize ELF note
282 static void *nt_init(void *buf, Elf64_Word type, void *desc, int d_len,
288 note = (Elf64_Nhdr *)buf;
289 note->n_namesz = strlen(name) + 1;
290 note->n_descsz = d_len;
292 len = sizeof(Elf64_Nhdr);
294 memcpy(buf + len, name, note->n_namesz);
295 len = roundup(len + note->n_namesz, 4);
297 memcpy(buf + len, desc, note->n_descsz);
298 len = roundup(len + note->n_descsz, 4);
300 return PTR_ADD(buf, len);
304 * Initialize prstatus note
306 static void *nt_prstatus(void *ptr, struct save_area *sa)
308 struct elf_prstatus nt_prstatus;
309 static int cpu_nr = 1;
311 memset(&nt_prstatus, 0, sizeof(nt_prstatus));
312 memcpy(&nt_prstatus.pr_reg.gprs, sa->gp_regs, sizeof(sa->gp_regs));
313 memcpy(&nt_prstatus.pr_reg.psw, sa->psw, sizeof(sa->psw));
314 memcpy(&nt_prstatus.pr_reg.acrs, sa->acc_regs, sizeof(sa->acc_regs));
315 nt_prstatus.pr_pid = cpu_nr;
318 return nt_init(ptr, NT_PRSTATUS, &nt_prstatus, sizeof(nt_prstatus),
323 * Initialize fpregset (floating point) note
325 static void *nt_fpregset(void *ptr, struct save_area *sa)
327 elf_fpregset_t nt_fpregset;
329 memset(&nt_fpregset, 0, sizeof(nt_fpregset));
330 memcpy(&nt_fpregset.fpc, &sa->fp_ctrl_reg, sizeof(sa->fp_ctrl_reg));
331 memcpy(&nt_fpregset.fprs, &sa->fp_regs, sizeof(sa->fp_regs));
333 return nt_init(ptr, NT_PRFPREG, &nt_fpregset, sizeof(nt_fpregset),
338 * Initialize timer note
340 static void *nt_s390_timer(void *ptr, struct save_area *sa)
342 return nt_init(ptr, NT_S390_TIMER, &sa->timer, sizeof(sa->timer),
343 KEXEC_CORE_NOTE_NAME);
347 * Initialize TOD clock comparator note
349 static void *nt_s390_tod_cmp(void *ptr, struct save_area *sa)
351 return nt_init(ptr, NT_S390_TODCMP, &sa->clk_cmp,
352 sizeof(sa->clk_cmp), KEXEC_CORE_NOTE_NAME);
356 * Initialize TOD programmable register note
358 static void *nt_s390_tod_preg(void *ptr, struct save_area *sa)
360 return nt_init(ptr, NT_S390_TODPREG, &sa->tod_reg,
361 sizeof(sa->tod_reg), KEXEC_CORE_NOTE_NAME);
365 * Initialize control register note
367 static void *nt_s390_ctrs(void *ptr, struct save_area *sa)
369 return nt_init(ptr, NT_S390_CTRS, &sa->ctrl_regs,
370 sizeof(sa->ctrl_regs), KEXEC_CORE_NOTE_NAME);
374 * Initialize prefix register note
376 static void *nt_s390_prefix(void *ptr, struct save_area *sa)
378 return nt_init(ptr, NT_S390_PREFIX, &sa->pref_reg,
379 sizeof(sa->pref_reg), KEXEC_CORE_NOTE_NAME);
383 * Fill ELF notes for one CPU with save area registers
385 void *fill_cpu_elf_notes(void *ptr, struct save_area *sa)
387 ptr = nt_prstatus(ptr, sa);
388 ptr = nt_fpregset(ptr, sa);
389 ptr = nt_s390_timer(ptr, sa);
390 ptr = nt_s390_tod_cmp(ptr, sa);
391 ptr = nt_s390_tod_preg(ptr, sa);
392 ptr = nt_s390_ctrs(ptr, sa);
393 ptr = nt_s390_prefix(ptr, sa);
398 * Initialize prpsinfo note (new kernel)
400 static void *nt_prpsinfo(void *ptr)
402 struct elf_prpsinfo prpsinfo;
404 memset(&prpsinfo, 0, sizeof(prpsinfo));
405 prpsinfo.pr_sname = 'R';
406 strcpy(prpsinfo.pr_fname, "vmlinux");
407 return nt_init(ptr, NT_PRPSINFO, &prpsinfo, sizeof(prpsinfo),
408 KEXEC_CORE_NOTE_NAME);
412 * Get vmcoreinfo using lowcore->vmcore_info (new kernel)
414 static void *get_vmcoreinfo_old(unsigned long *size)
416 char nt_name[11], *vmcoreinfo;
420 if (copy_from_oldmem(&addr, &S390_lowcore.vmcore_info, sizeof(addr)))
422 memset(nt_name, 0, sizeof(nt_name));
423 if (copy_from_oldmem(¬e, addr, sizeof(note)))
425 if (copy_from_oldmem(nt_name, addr + sizeof(note), sizeof(nt_name) - 1))
427 if (strcmp(nt_name, "VMCOREINFO") != 0)
429 vmcoreinfo = kzalloc_panic(note.n_descsz);
430 if (copy_from_oldmem(vmcoreinfo, addr + 24, note.n_descsz))
432 *size = note.n_descsz;
437 * Initialize vmcoreinfo note (new kernel)
439 static void *nt_vmcoreinfo(void *ptr)
444 vmcoreinfo = os_info_old_entry(OS_INFO_VMCOREINFO, &size);
446 vmcoreinfo = get_vmcoreinfo_old(&size);
449 return nt_init(ptr, 0, vmcoreinfo, size, "VMCOREINFO");
453 * Initialize ELF header (new kernel)
455 static void *ehdr_init(Elf64_Ehdr *ehdr, int mem_chunk_cnt)
457 memset(ehdr, 0, sizeof(*ehdr));
458 memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
459 ehdr->e_ident[EI_CLASS] = ELFCLASS64;
460 ehdr->e_ident[EI_DATA] = ELFDATA2MSB;
461 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
462 memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
463 ehdr->e_type = ET_CORE;
464 ehdr->e_machine = EM_S390;
465 ehdr->e_version = EV_CURRENT;
466 ehdr->e_phoff = sizeof(Elf64_Ehdr);
467 ehdr->e_ehsize = sizeof(Elf64_Ehdr);
468 ehdr->e_phentsize = sizeof(Elf64_Phdr);
469 ehdr->e_phnum = mem_chunk_cnt + 1;
474 * Return CPU count for ELF header (new kernel)
476 static int get_cpu_cnt(void)
480 for (i = 0; i < dump_save_areas.count; i++) {
481 if (dump_save_areas.areas[i]->pref_reg == 0)
489 * Return memory chunk count for ELF header (new kernel)
491 static int get_mem_chunk_cnt(void)
493 struct mem_chunk *chunk_array, *mem_chunk;
496 chunk_array = get_memory_layout();
497 for (i = 0; i < MEMORY_CHUNKS; i++) {
498 mem_chunk = &chunk_array[i];
499 if (chunk_array[i].type != CHUNK_READ_WRITE &&
500 chunk_array[i].type != CHUNK_READ_ONLY)
502 if (mem_chunk->size == 0)
511 * Initialize ELF loads (new kernel)
513 static int loads_init(Elf64_Phdr *phdr, u64 loads_offset)
515 struct mem_chunk *chunk_array, *mem_chunk;
518 chunk_array = get_memory_layout();
519 for (i = 0; i < MEMORY_CHUNKS; i++) {
520 mem_chunk = &chunk_array[i];
521 if (mem_chunk->size == 0)
523 if (chunk_array[i].type != CHUNK_READ_WRITE &&
524 chunk_array[i].type != CHUNK_READ_ONLY)
527 phdr->p_filesz = mem_chunk->size;
528 phdr->p_type = PT_LOAD;
529 phdr->p_offset = mem_chunk->addr;
530 phdr->p_vaddr = mem_chunk->addr;
531 phdr->p_paddr = mem_chunk->addr;
532 phdr->p_memsz = mem_chunk->size;
533 phdr->p_flags = PF_R | PF_W | PF_X;
534 phdr->p_align = PAGE_SIZE;
542 * Initialize notes (new kernel)
544 static void *notes_init(Elf64_Phdr *phdr, void *ptr, u64 notes_offset)
546 struct save_area *sa;
547 void *ptr_start = ptr;
550 ptr = nt_prpsinfo(ptr);
552 for (i = 0; i < dump_save_areas.count; i++) {
553 sa = dump_save_areas.areas[i];
554 if (sa->pref_reg == 0)
556 ptr = fill_cpu_elf_notes(ptr, sa);
558 ptr = nt_vmcoreinfo(ptr);
559 memset(phdr, 0, sizeof(*phdr));
560 phdr->p_type = PT_NOTE;
561 phdr->p_offset = notes_offset;
562 phdr->p_filesz = (unsigned long) PTR_SUB(ptr, ptr_start);
563 phdr->p_memsz = phdr->p_filesz;
568 * Create ELF core header (new kernel)
570 int elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size)
572 Elf64_Phdr *phdr_notes, *phdr_loads;
578 /* If we are not in kdump or zfcpdump mode return */
579 if (!OLDMEM_BASE && ipl_info.type != IPL_TYPE_FCP_DUMP)
581 /* If elfcorehdr= has been passed via cmdline, we use that one */
582 if (elfcorehdr_addr != ELFCORE_ADDR_MAX)
584 /* If we cannot get HSA size for zfcpdump return error */
585 if (ipl_info.type == IPL_TYPE_FCP_DUMP && !sclp_get_hsa_size())
587 mem_chunk_cnt = get_mem_chunk_cnt();
589 alloc_size = 0x1000 + get_cpu_cnt() * 0x300 +
590 mem_chunk_cnt * sizeof(Elf64_Phdr);
591 hdr = kzalloc_panic(alloc_size);
592 /* Init elf header */
593 ptr = ehdr_init(hdr, mem_chunk_cnt);
594 /* Init program headers */
596 ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr));
598 ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr) * mem_chunk_cnt);
600 hdr_off = PTR_DIFF(ptr, hdr);
601 ptr = notes_init(phdr_notes, ptr, ((unsigned long) hdr) + hdr_off);
603 hdr_off = PTR_DIFF(ptr, hdr);
604 loads_init(phdr_loads, hdr_off);
605 *addr = (unsigned long long) hdr;
606 elfcorehdr_newmem = hdr;
607 *size = (unsigned long long) hdr_off;
608 BUG_ON(elfcorehdr_size > alloc_size);
613 * Free ELF core header (new kernel)
615 void elfcorehdr_free(unsigned long long addr)
617 if (!elfcorehdr_newmem)
619 kfree((void *)(unsigned long)addr);
623 * Read from ELF header
625 ssize_t elfcorehdr_read(char *buf, size_t count, u64 *ppos)
627 void *src = (void *)(unsigned long)*ppos;
629 src = elfcorehdr_newmem ? src : src - OLDMEM_BASE;
630 memcpy(buf, src, count);
636 * Read from ELF notes data
638 ssize_t elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos)
640 void *src = (void *)(unsigned long)*ppos;
643 if (elfcorehdr_newmem) {
644 memcpy(buf, src, count);
646 rc = copy_from_oldmem(buf, src, count);