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))))
27 * Return physical address for virtual address
29 static inline void *load_real_addr(void *addr)
31 unsigned long real_addr;
38 : "=a" (real_addr) : "a" (addr) : "cc");
39 return (void *)real_addr;
43 * Copy up to one page to vmalloc or real memory
45 static ssize_t copy_page_real(void *buf, void *src, size_t csize)
49 if (is_vmalloc_addr(buf)) {
50 BUG_ON(csize >= PAGE_SIZE);
51 /* If buf is not page aligned, copy first part */
52 size = min(roundup(__pa(buf), PAGE_SIZE) - __pa(buf), csize);
54 if (memcpy_real(load_real_addr(buf), src, size))
59 /* Copy second part */
61 return (size) ? memcpy_real(load_real_addr(buf), src, size) : 0;
63 return memcpy_real(buf, src, csize);
68 * Pointer to ELF header in new kernel
70 static void *elfcorehdr_newmem;
73 * Copy one page from zfcpdump "oldmem"
75 * For pages below ZFCPDUMP_HSA_SIZE memory from the HSA is copied. Otherwise
76 * real memory copy is used.
78 static ssize_t copy_oldmem_page_zfcpdump(char *buf, size_t csize,
79 unsigned long src, int userbuf)
83 if (src < ZFCPDUMP_HSA_SIZE) {
84 rc = memcpy_hsa(buf, src, csize, userbuf);
87 rc = copy_to_user_real((void __force __user *) buf,
90 rc = memcpy_real(buf, (void *) src, csize);
92 return rc ? rc : csize;
96 * Copy one page from kdump "oldmem"
98 * For the kdump reserved memory this functions performs a swap operation:
99 * - [OLDMEM_BASE - OLDMEM_BASE + OLDMEM_SIZE] is mapped to [0 - OLDMEM_SIZE].
100 * - [0 - OLDMEM_SIZE] is mapped to [OLDMEM_BASE - OLDMEM_BASE + OLDMEM_SIZE]
102 static ssize_t copy_oldmem_page_kdump(char *buf, size_t csize,
103 unsigned long src, int userbuf)
108 if (src < OLDMEM_SIZE)
110 else if (src > OLDMEM_BASE &&
111 src < OLDMEM_BASE + OLDMEM_SIZE)
114 rc = copy_to_user_real((void __force __user *) buf,
115 (void *) src, csize);
117 rc = copy_page_real(buf, (void *) src, csize);
118 return (rc == 0) ? rc : csize;
122 * Copy one page from "oldmem"
124 ssize_t copy_oldmem_page(unsigned long pfn, char *buf, size_t csize,
125 unsigned long offset, int userbuf)
131 src = (pfn << PAGE_SHIFT) + offset;
133 return copy_oldmem_page_kdump(buf, csize, src, userbuf);
135 return copy_oldmem_page_zfcpdump(buf, csize, src, userbuf);
139 * Remap "oldmem" for kdump
141 * For the kdump reserved memory this functions performs a swap operation:
142 * [0 - OLDMEM_SIZE] is mapped to [OLDMEM_BASE - OLDMEM_BASE + OLDMEM_SIZE]
144 static int remap_oldmem_pfn_range_kdump(struct vm_area_struct *vma,
145 unsigned long from, unsigned long pfn,
146 unsigned long size, pgprot_t prot)
148 unsigned long size_old;
151 if (pfn < OLDMEM_SIZE >> PAGE_SHIFT) {
152 size_old = min(size, OLDMEM_SIZE - (pfn << PAGE_SHIFT));
153 rc = remap_pfn_range(vma, from,
154 pfn + (OLDMEM_BASE >> PAGE_SHIFT),
156 if (rc || size == size_old)
160 pfn += size_old >> PAGE_SHIFT;
162 return remap_pfn_range(vma, from, pfn, size, prot);
166 * Remap "oldmem" for zfcpdump
168 * We only map available memory above ZFCPDUMP_HSA_SIZE. Memory below
169 * ZFCPDUMP_HSA_SIZE is read on demand using the copy_oldmem_page() function.
171 static int remap_oldmem_pfn_range_zfcpdump(struct vm_area_struct *vma,
174 unsigned long size, pgprot_t prot)
176 unsigned long size_hsa;
178 if (pfn < ZFCPDUMP_HSA_SIZE >> PAGE_SHIFT) {
179 size_hsa = min(size, ZFCPDUMP_HSA_SIZE - (pfn << PAGE_SHIFT));
180 if (size == size_hsa)
184 pfn += size_hsa >> PAGE_SHIFT;
186 return remap_pfn_range(vma, from, pfn, size, prot);
190 * Remap "oldmem" for kdump or zfcpdump
192 int remap_oldmem_pfn_range(struct vm_area_struct *vma, unsigned long from,
193 unsigned long pfn, unsigned long size, pgprot_t prot)
196 return remap_oldmem_pfn_range_kdump(vma, from, pfn, size, prot);
198 return remap_oldmem_pfn_range_zfcpdump(vma, from, pfn, size,
203 * Copy memory from old kernel
205 int copy_from_oldmem(void *dest, void *src, size_t count)
207 unsigned long copied = 0;
211 if ((unsigned long) src < OLDMEM_SIZE) {
212 copied = min(count, OLDMEM_SIZE - (unsigned long) src);
213 rc = memcpy_real(dest, src + OLDMEM_BASE, copied);
218 if ((unsigned long) src < ZFCPDUMP_HSA_SIZE) {
220 ZFCPDUMP_HSA_SIZE - (unsigned long) src);
221 rc = memcpy_hsa(dest, (unsigned long) src, copied, 0);
226 return memcpy_real(dest + copied, src + copied, count - copied);
230 * Alloc memory and panic in case of ENOMEM
232 static void *kzalloc_panic(int len)
236 rc = kzalloc(len, GFP_KERNEL);
238 panic("s390 kdump kzalloc (%d) failed", len);
243 * Get memory layout and create hole for oldmem
245 static struct mem_chunk *get_memory_layout(void)
247 struct mem_chunk *chunk_array;
249 chunk_array = kzalloc_panic(MEMORY_CHUNKS * sizeof(struct mem_chunk));
250 detect_memory_layout(chunk_array, 0);
251 create_mem_hole(chunk_array, OLDMEM_BASE, OLDMEM_SIZE);
256 * Initialize ELF note
258 static void *nt_init(void *buf, Elf64_Word type, void *desc, int d_len,
264 note = (Elf64_Nhdr *)buf;
265 note->n_namesz = strlen(name) + 1;
266 note->n_descsz = d_len;
268 len = sizeof(Elf64_Nhdr);
270 memcpy(buf + len, name, note->n_namesz);
271 len = roundup(len + note->n_namesz, 4);
273 memcpy(buf + len, desc, note->n_descsz);
274 len = roundup(len + note->n_descsz, 4);
276 return PTR_ADD(buf, len);
280 * Initialize prstatus note
282 static void *nt_prstatus(void *ptr, struct save_area *sa)
284 struct elf_prstatus nt_prstatus;
285 static int cpu_nr = 1;
287 memset(&nt_prstatus, 0, sizeof(nt_prstatus));
288 memcpy(&nt_prstatus.pr_reg.gprs, sa->gp_regs, sizeof(sa->gp_regs));
289 memcpy(&nt_prstatus.pr_reg.psw, sa->psw, sizeof(sa->psw));
290 memcpy(&nt_prstatus.pr_reg.acrs, sa->acc_regs, sizeof(sa->acc_regs));
291 nt_prstatus.pr_pid = cpu_nr;
294 return nt_init(ptr, NT_PRSTATUS, &nt_prstatus, sizeof(nt_prstatus),
299 * Initialize fpregset (floating point) note
301 static void *nt_fpregset(void *ptr, struct save_area *sa)
303 elf_fpregset_t nt_fpregset;
305 memset(&nt_fpregset, 0, sizeof(nt_fpregset));
306 memcpy(&nt_fpregset.fpc, &sa->fp_ctrl_reg, sizeof(sa->fp_ctrl_reg));
307 memcpy(&nt_fpregset.fprs, &sa->fp_regs, sizeof(sa->fp_regs));
309 return nt_init(ptr, NT_PRFPREG, &nt_fpregset, sizeof(nt_fpregset),
314 * Initialize timer note
316 static void *nt_s390_timer(void *ptr, struct save_area *sa)
318 return nt_init(ptr, NT_S390_TIMER, &sa->timer, sizeof(sa->timer),
319 KEXEC_CORE_NOTE_NAME);
323 * Initialize TOD clock comparator note
325 static void *nt_s390_tod_cmp(void *ptr, struct save_area *sa)
327 return nt_init(ptr, NT_S390_TODCMP, &sa->clk_cmp,
328 sizeof(sa->clk_cmp), KEXEC_CORE_NOTE_NAME);
332 * Initialize TOD programmable register note
334 static void *nt_s390_tod_preg(void *ptr, struct save_area *sa)
336 return nt_init(ptr, NT_S390_TODPREG, &sa->tod_reg,
337 sizeof(sa->tod_reg), KEXEC_CORE_NOTE_NAME);
341 * Initialize control register note
343 static void *nt_s390_ctrs(void *ptr, struct save_area *sa)
345 return nt_init(ptr, NT_S390_CTRS, &sa->ctrl_regs,
346 sizeof(sa->ctrl_regs), KEXEC_CORE_NOTE_NAME);
350 * Initialize prefix register note
352 static void *nt_s390_prefix(void *ptr, struct save_area *sa)
354 return nt_init(ptr, NT_S390_PREFIX, &sa->pref_reg,
355 sizeof(sa->pref_reg), KEXEC_CORE_NOTE_NAME);
359 * Fill ELF notes for one CPU with save area registers
361 void *fill_cpu_elf_notes(void *ptr, struct save_area *sa)
363 ptr = nt_prstatus(ptr, sa);
364 ptr = nt_fpregset(ptr, sa);
365 ptr = nt_s390_timer(ptr, sa);
366 ptr = nt_s390_tod_cmp(ptr, sa);
367 ptr = nt_s390_tod_preg(ptr, sa);
368 ptr = nt_s390_ctrs(ptr, sa);
369 ptr = nt_s390_prefix(ptr, sa);
374 * Initialize prpsinfo note (new kernel)
376 static void *nt_prpsinfo(void *ptr)
378 struct elf_prpsinfo prpsinfo;
380 memset(&prpsinfo, 0, sizeof(prpsinfo));
381 prpsinfo.pr_sname = 'R';
382 strcpy(prpsinfo.pr_fname, "vmlinux");
383 return nt_init(ptr, NT_PRPSINFO, &prpsinfo, sizeof(prpsinfo),
384 KEXEC_CORE_NOTE_NAME);
388 * Get vmcoreinfo using lowcore->vmcore_info (new kernel)
390 static void *get_vmcoreinfo_old(unsigned long *size)
392 char nt_name[11], *vmcoreinfo;
396 if (copy_from_oldmem(&addr, &S390_lowcore.vmcore_info, sizeof(addr)))
398 memset(nt_name, 0, sizeof(nt_name));
399 if (copy_from_oldmem(¬e, addr, sizeof(note)))
401 if (copy_from_oldmem(nt_name, addr + sizeof(note), sizeof(nt_name) - 1))
403 if (strcmp(nt_name, "VMCOREINFO") != 0)
405 vmcoreinfo = kzalloc_panic(note.n_descsz);
406 if (copy_from_oldmem(vmcoreinfo, addr + 24, note.n_descsz))
408 *size = note.n_descsz;
413 * Initialize vmcoreinfo note (new kernel)
415 static void *nt_vmcoreinfo(void *ptr)
420 vmcoreinfo = os_info_old_entry(OS_INFO_VMCOREINFO, &size);
422 vmcoreinfo = get_vmcoreinfo_old(&size);
425 return nt_init(ptr, 0, vmcoreinfo, size, "VMCOREINFO");
429 * Initialize ELF header (new kernel)
431 static void *ehdr_init(Elf64_Ehdr *ehdr, int mem_chunk_cnt)
433 memset(ehdr, 0, sizeof(*ehdr));
434 memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
435 ehdr->e_ident[EI_CLASS] = ELFCLASS64;
436 ehdr->e_ident[EI_DATA] = ELFDATA2MSB;
437 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
438 memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
439 ehdr->e_type = ET_CORE;
440 ehdr->e_machine = EM_S390;
441 ehdr->e_version = EV_CURRENT;
442 ehdr->e_phoff = sizeof(Elf64_Ehdr);
443 ehdr->e_ehsize = sizeof(Elf64_Ehdr);
444 ehdr->e_phentsize = sizeof(Elf64_Phdr);
445 ehdr->e_phnum = mem_chunk_cnt + 1;
450 * Return CPU count for ELF header (new kernel)
452 static int get_cpu_cnt(void)
456 for (i = 0; zfcpdump_save_areas[i]; i++) {
457 if (zfcpdump_save_areas[i]->pref_reg == 0)
465 * Return memory chunk count for ELF header (new kernel)
467 static int get_mem_chunk_cnt(void)
469 struct mem_chunk *chunk_array, *mem_chunk;
472 chunk_array = get_memory_layout();
473 for (i = 0; i < MEMORY_CHUNKS; i++) {
474 mem_chunk = &chunk_array[i];
475 if (chunk_array[i].type != CHUNK_READ_WRITE &&
476 chunk_array[i].type != CHUNK_READ_ONLY)
478 if (mem_chunk->size == 0)
487 * Initialize ELF loads (new kernel)
489 static int loads_init(Elf64_Phdr *phdr, u64 loads_offset)
491 struct mem_chunk *chunk_array, *mem_chunk;
494 chunk_array = get_memory_layout();
495 for (i = 0; i < MEMORY_CHUNKS; i++) {
496 mem_chunk = &chunk_array[i];
497 if (mem_chunk->size == 0)
499 if (chunk_array[i].type != CHUNK_READ_WRITE &&
500 chunk_array[i].type != CHUNK_READ_ONLY)
503 phdr->p_filesz = mem_chunk->size;
504 phdr->p_type = PT_LOAD;
505 phdr->p_offset = mem_chunk->addr;
506 phdr->p_vaddr = mem_chunk->addr;
507 phdr->p_paddr = mem_chunk->addr;
508 phdr->p_memsz = mem_chunk->size;
509 phdr->p_flags = PF_R | PF_W | PF_X;
510 phdr->p_align = PAGE_SIZE;
518 * Initialize notes (new kernel)
520 static void *notes_init(Elf64_Phdr *phdr, void *ptr, u64 notes_offset)
522 struct save_area *sa;
523 void *ptr_start = ptr;
526 ptr = nt_prpsinfo(ptr);
528 for (i = 0; zfcpdump_save_areas[i]; i++) {
529 sa = zfcpdump_save_areas[i];
530 if (sa->pref_reg == 0)
532 ptr = fill_cpu_elf_notes(ptr, sa);
534 ptr = nt_vmcoreinfo(ptr);
535 memset(phdr, 0, sizeof(*phdr));
536 phdr->p_type = PT_NOTE;
537 phdr->p_offset = notes_offset;
538 phdr->p_filesz = (unsigned long) PTR_SUB(ptr, ptr_start);
539 phdr->p_memsz = phdr->p_filesz;
544 * Create ELF core header (new kernel)
546 int elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size)
548 Elf64_Phdr *phdr_notes, *phdr_loads;
554 /* If we are not in kdump or zfcpdump mode return */
555 if (!OLDMEM_BASE && ipl_info.type != IPL_TYPE_FCP_DUMP)
557 /* If elfcorehdr= has been passed via cmdline, we use that one */
558 if (elfcorehdr_addr != ELFCORE_ADDR_MAX)
560 mem_chunk_cnt = get_mem_chunk_cnt();
562 alloc_size = 0x1000 + get_cpu_cnt() * 0x300 +
563 mem_chunk_cnt * sizeof(Elf64_Phdr);
564 hdr = kzalloc_panic(alloc_size);
565 /* Init elf header */
566 ptr = ehdr_init(hdr, mem_chunk_cnt);
567 /* Init program headers */
569 ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr));
571 ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr) * mem_chunk_cnt);
573 hdr_off = PTR_DIFF(ptr, hdr);
574 ptr = notes_init(phdr_notes, ptr, ((unsigned long) hdr) + hdr_off);
576 hdr_off = PTR_DIFF(ptr, hdr);
577 loads_init(phdr_loads, hdr_off);
578 *addr = (unsigned long long) hdr;
579 elfcorehdr_newmem = hdr;
580 *size = (unsigned long long) hdr_off;
581 BUG_ON(elfcorehdr_size > alloc_size);
586 * Free ELF core header (new kernel)
588 void elfcorehdr_free(unsigned long long addr)
590 if (!elfcorehdr_newmem)
592 kfree((void *)(unsigned long)addr);
596 * Read from ELF header
598 ssize_t elfcorehdr_read(char *buf, size_t count, u64 *ppos)
600 void *src = (void *)(unsigned long)*ppos;
602 src = elfcorehdr_newmem ? src : src - OLDMEM_BASE;
603 memcpy(buf, src, count);
609 * Read from ELF notes data
611 ssize_t elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos)
613 void *src = (void *)(unsigned long)*ppos;
616 if (elfcorehdr_newmem) {
617 memcpy(buf, src, count);
619 rc = copy_from_oldmem(buf, src, count);