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 ZFCPDUMP_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 < ZFCPDUMP_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 ZFCPDUMP_HSA_SIZE. Memory below
192 * ZFCPDUMP_HSA_SIZE 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 size_hsa;
201 if (pfn < ZFCPDUMP_HSA_SIZE >> PAGE_SHIFT) {
202 size_hsa = min(size, ZFCPDUMP_HSA_SIZE - (pfn << PAGE_SHIFT));
203 if (size == size_hsa)
207 pfn += size_hsa >> PAGE_SHIFT;
209 return remap_pfn_range(vma, from, pfn, size, prot);
213 * Remap "oldmem" for kdump or zfcpdump
215 int remap_oldmem_pfn_range(struct vm_area_struct *vma, unsigned long from,
216 unsigned long pfn, unsigned long size, pgprot_t prot)
219 return remap_oldmem_pfn_range_kdump(vma, from, pfn, size, prot);
221 return remap_oldmem_pfn_range_zfcpdump(vma, from, pfn, size,
226 * Copy memory from old kernel
228 int copy_from_oldmem(void *dest, void *src, size_t count)
230 unsigned long copied = 0;
234 if ((unsigned long) src < OLDMEM_SIZE) {
235 copied = min(count, OLDMEM_SIZE - (unsigned long) src);
236 rc = copy_from_realmem(dest, src + OLDMEM_BASE, copied);
241 if ((unsigned long) src < ZFCPDUMP_HSA_SIZE) {
243 ZFCPDUMP_HSA_SIZE - (unsigned long) src);
244 rc = memcpy_hsa(dest, (unsigned long) src, copied, 0);
249 return copy_from_realmem(dest + copied, src + copied, count - copied);
253 * Alloc memory and panic in case of ENOMEM
255 static void *kzalloc_panic(int len)
259 rc = kzalloc(len, GFP_KERNEL);
261 panic("s390 kdump kzalloc (%d) failed", len);
266 * Get memory layout and create hole for oldmem
268 static struct mem_chunk *get_memory_layout(void)
270 struct mem_chunk *chunk_array;
272 chunk_array = kzalloc_panic(MEMORY_CHUNKS * sizeof(struct mem_chunk));
273 detect_memory_layout(chunk_array, 0);
274 create_mem_hole(chunk_array, OLDMEM_BASE, OLDMEM_SIZE);
279 * Initialize ELF note
281 static void *nt_init(void *buf, Elf64_Word type, void *desc, int d_len,
287 note = (Elf64_Nhdr *)buf;
288 note->n_namesz = strlen(name) + 1;
289 note->n_descsz = d_len;
291 len = sizeof(Elf64_Nhdr);
293 memcpy(buf + len, name, note->n_namesz);
294 len = roundup(len + note->n_namesz, 4);
296 memcpy(buf + len, desc, note->n_descsz);
297 len = roundup(len + note->n_descsz, 4);
299 return PTR_ADD(buf, len);
303 * Initialize prstatus note
305 static void *nt_prstatus(void *ptr, struct save_area *sa)
307 struct elf_prstatus nt_prstatus;
308 static int cpu_nr = 1;
310 memset(&nt_prstatus, 0, sizeof(nt_prstatus));
311 memcpy(&nt_prstatus.pr_reg.gprs, sa->gp_regs, sizeof(sa->gp_regs));
312 memcpy(&nt_prstatus.pr_reg.psw, sa->psw, sizeof(sa->psw));
313 memcpy(&nt_prstatus.pr_reg.acrs, sa->acc_regs, sizeof(sa->acc_regs));
314 nt_prstatus.pr_pid = cpu_nr;
317 return nt_init(ptr, NT_PRSTATUS, &nt_prstatus, sizeof(nt_prstatus),
322 * Initialize fpregset (floating point) note
324 static void *nt_fpregset(void *ptr, struct save_area *sa)
326 elf_fpregset_t nt_fpregset;
328 memset(&nt_fpregset, 0, sizeof(nt_fpregset));
329 memcpy(&nt_fpregset.fpc, &sa->fp_ctrl_reg, sizeof(sa->fp_ctrl_reg));
330 memcpy(&nt_fpregset.fprs, &sa->fp_regs, sizeof(sa->fp_regs));
332 return nt_init(ptr, NT_PRFPREG, &nt_fpregset, sizeof(nt_fpregset),
337 * Initialize timer note
339 static void *nt_s390_timer(void *ptr, struct save_area *sa)
341 return nt_init(ptr, NT_S390_TIMER, &sa->timer, sizeof(sa->timer),
342 KEXEC_CORE_NOTE_NAME);
346 * Initialize TOD clock comparator note
348 static void *nt_s390_tod_cmp(void *ptr, struct save_area *sa)
350 return nt_init(ptr, NT_S390_TODCMP, &sa->clk_cmp,
351 sizeof(sa->clk_cmp), KEXEC_CORE_NOTE_NAME);
355 * Initialize TOD programmable register note
357 static void *nt_s390_tod_preg(void *ptr, struct save_area *sa)
359 return nt_init(ptr, NT_S390_TODPREG, &sa->tod_reg,
360 sizeof(sa->tod_reg), KEXEC_CORE_NOTE_NAME);
364 * Initialize control register note
366 static void *nt_s390_ctrs(void *ptr, struct save_area *sa)
368 return nt_init(ptr, NT_S390_CTRS, &sa->ctrl_regs,
369 sizeof(sa->ctrl_regs), KEXEC_CORE_NOTE_NAME);
373 * Initialize prefix register note
375 static void *nt_s390_prefix(void *ptr, struct save_area *sa)
377 return nt_init(ptr, NT_S390_PREFIX, &sa->pref_reg,
378 sizeof(sa->pref_reg), KEXEC_CORE_NOTE_NAME);
382 * Fill ELF notes for one CPU with save area registers
384 void *fill_cpu_elf_notes(void *ptr, struct save_area *sa)
386 ptr = nt_prstatus(ptr, sa);
387 ptr = nt_fpregset(ptr, sa);
388 ptr = nt_s390_timer(ptr, sa);
389 ptr = nt_s390_tod_cmp(ptr, sa);
390 ptr = nt_s390_tod_preg(ptr, sa);
391 ptr = nt_s390_ctrs(ptr, sa);
392 ptr = nt_s390_prefix(ptr, sa);
397 * Initialize prpsinfo note (new kernel)
399 static void *nt_prpsinfo(void *ptr)
401 struct elf_prpsinfo prpsinfo;
403 memset(&prpsinfo, 0, sizeof(prpsinfo));
404 prpsinfo.pr_sname = 'R';
405 strcpy(prpsinfo.pr_fname, "vmlinux");
406 return nt_init(ptr, NT_PRPSINFO, &prpsinfo, sizeof(prpsinfo),
407 KEXEC_CORE_NOTE_NAME);
411 * Get vmcoreinfo using lowcore->vmcore_info (new kernel)
413 static void *get_vmcoreinfo_old(unsigned long *size)
415 char nt_name[11], *vmcoreinfo;
419 if (copy_from_oldmem(&addr, &S390_lowcore.vmcore_info, sizeof(addr)))
421 memset(nt_name, 0, sizeof(nt_name));
422 if (copy_from_oldmem(¬e, addr, sizeof(note)))
424 if (copy_from_oldmem(nt_name, addr + sizeof(note), sizeof(nt_name) - 1))
426 if (strcmp(nt_name, "VMCOREINFO") != 0)
428 vmcoreinfo = kzalloc_panic(note.n_descsz);
429 if (copy_from_oldmem(vmcoreinfo, addr + 24, note.n_descsz))
431 *size = note.n_descsz;
436 * Initialize vmcoreinfo note (new kernel)
438 static void *nt_vmcoreinfo(void *ptr)
443 vmcoreinfo = os_info_old_entry(OS_INFO_VMCOREINFO, &size);
445 vmcoreinfo = get_vmcoreinfo_old(&size);
448 return nt_init(ptr, 0, vmcoreinfo, size, "VMCOREINFO");
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);
468 ehdr->e_phnum = mem_chunk_cnt + 1;
473 * Return CPU count for ELF header (new kernel)
475 static int get_cpu_cnt(void)
479 for (i = 0; i < dump_save_areas.count; i++) {
480 if (dump_save_areas.areas[i]->pref_reg == 0)
488 * Return memory chunk count for ELF header (new kernel)
490 static int get_mem_chunk_cnt(void)
492 struct mem_chunk *chunk_array, *mem_chunk;
495 chunk_array = get_memory_layout();
496 for (i = 0; i < MEMORY_CHUNKS; i++) {
497 mem_chunk = &chunk_array[i];
498 if (chunk_array[i].type != CHUNK_READ_WRITE &&
499 chunk_array[i].type != CHUNK_READ_ONLY)
501 if (mem_chunk->size == 0)
510 * Initialize ELF loads (new kernel)
512 static int loads_init(Elf64_Phdr *phdr, u64 loads_offset)
514 struct mem_chunk *chunk_array, *mem_chunk;
517 chunk_array = get_memory_layout();
518 for (i = 0; i < MEMORY_CHUNKS; i++) {
519 mem_chunk = &chunk_array[i];
520 if (mem_chunk->size == 0)
522 if (chunk_array[i].type != CHUNK_READ_WRITE &&
523 chunk_array[i].type != CHUNK_READ_ONLY)
526 phdr->p_filesz = mem_chunk->size;
527 phdr->p_type = PT_LOAD;
528 phdr->p_offset = mem_chunk->addr;
529 phdr->p_vaddr = mem_chunk->addr;
530 phdr->p_paddr = mem_chunk->addr;
531 phdr->p_memsz = mem_chunk->size;
532 phdr->p_flags = PF_R | PF_W | PF_X;
533 phdr->p_align = PAGE_SIZE;
541 * Initialize notes (new kernel)
543 static void *notes_init(Elf64_Phdr *phdr, void *ptr, u64 notes_offset)
545 struct save_area *sa;
546 void *ptr_start = ptr;
549 ptr = nt_prpsinfo(ptr);
551 for (i = 0; i < dump_save_areas.count; i++) {
552 sa = dump_save_areas.areas[i];
553 if (sa->pref_reg == 0)
555 ptr = fill_cpu_elf_notes(ptr, sa);
557 ptr = nt_vmcoreinfo(ptr);
558 memset(phdr, 0, sizeof(*phdr));
559 phdr->p_type = PT_NOTE;
560 phdr->p_offset = notes_offset;
561 phdr->p_filesz = (unsigned long) PTR_SUB(ptr, ptr_start);
562 phdr->p_memsz = phdr->p_filesz;
567 * Create ELF core header (new kernel)
569 int elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size)
571 Elf64_Phdr *phdr_notes, *phdr_loads;
577 /* If we are not in kdump or zfcpdump mode return */
578 if (!OLDMEM_BASE && ipl_info.type != IPL_TYPE_FCP_DUMP)
580 /* If elfcorehdr= has been passed via cmdline, we use that one */
581 if (elfcorehdr_addr != ELFCORE_ADDR_MAX)
583 mem_chunk_cnt = get_mem_chunk_cnt();
585 alloc_size = 0x1000 + get_cpu_cnt() * 0x300 +
586 mem_chunk_cnt * sizeof(Elf64_Phdr);
587 hdr = kzalloc_panic(alloc_size);
588 /* Init elf header */
589 ptr = ehdr_init(hdr, mem_chunk_cnt);
590 /* Init program headers */
592 ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr));
594 ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr) * mem_chunk_cnt);
596 hdr_off = PTR_DIFF(ptr, hdr);
597 ptr = notes_init(phdr_notes, ptr, ((unsigned long) hdr) + hdr_off);
599 hdr_off = PTR_DIFF(ptr, hdr);
600 loads_init(phdr_loads, hdr_off);
601 *addr = (unsigned long long) hdr;
602 elfcorehdr_newmem = hdr;
603 *size = (unsigned long long) hdr_off;
604 BUG_ON(elfcorehdr_size > alloc_size);
609 * Free ELF core header (new kernel)
611 void elfcorehdr_free(unsigned long long addr)
613 if (!elfcorehdr_newmem)
615 kfree((void *)(unsigned long)addr);
619 * Read from ELF header
621 ssize_t elfcorehdr_read(char *buf, size_t count, u64 *ppos)
623 void *src = (void *)(unsigned long)*ppos;
625 src = elfcorehdr_newmem ? src : src - OLDMEM_BASE;
626 memcpy(buf, src, count);
632 * Read from ELF notes data
634 ssize_t elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos)
636 void *src = (void *)(unsigned long)*ppos;
639 if (elfcorehdr_newmem) {
640 memcpy(buf, src, count);
642 rc = copy_from_oldmem(buf, src, count);