of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / arch / s390 / kernel / crash_dump.c
blob171e09bb8ea2a0b0e6ad23d68ff6e6617a83e8bc
1 /*
2 * S390 kdump implementation
4 * Copyright IBM Corp. 2011
5 * Author(s): Michael Holzheu <holzheu@linux.vnet.ibm.com>
6 */
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 <linux/memblock.h>
17 #include <asm/os_info.h>
18 #include <asm/elf.h>
19 #include <asm/ipl.h>
20 #include <asm/sclp.h>
22 #define PTR_ADD(x, y) (((char *) (x)) + ((unsigned long) (y)))
23 #define PTR_SUB(x, y) (((char *) (x)) - ((unsigned long) (y)))
24 #define PTR_DIFF(x, y) ((unsigned long)(((char *) (x)) - ((unsigned long) (y))))
26 static struct memblock_region oldmem_region;
28 static struct memblock_type oldmem_type = {
29 .cnt = 1,
30 .max = 1,
31 .total_size = 0,
32 .regions = &oldmem_region,
35 struct dump_save_areas dump_save_areas;
38 * Return physical address for virtual address
40 static inline void *load_real_addr(void *addr)
42 unsigned long real_addr;
44 asm volatile(
45 " lra %0,0(%1)\n"
46 " jz 0f\n"
47 " la %0,0\n"
48 "0:"
49 : "=a" (real_addr) : "a" (addr) : "cc");
50 return (void *)real_addr;
54 * Copy real to virtual or real memory
56 static int copy_from_realmem(void *dest, void *src, size_t count)
58 unsigned long size;
60 if (!count)
61 return 0;
62 if (!is_vmalloc_or_module_addr(dest))
63 return memcpy_real(dest, src, count);
64 do {
65 size = min(count, PAGE_SIZE - (__pa(dest) & ~PAGE_MASK));
66 if (memcpy_real(load_real_addr(dest), src, size))
67 return -EFAULT;
68 count -= size;
69 dest += size;
70 src += size;
71 } while (count);
72 return 0;
76 * Pointer to ELF header in new kernel
78 static void *elfcorehdr_newmem;
81 * Copy one page from zfcpdump "oldmem"
83 * For pages below HSA size memory from the HSA is copied. Otherwise
84 * real memory copy is used.
86 static ssize_t copy_oldmem_page_zfcpdump(char *buf, size_t csize,
87 unsigned long src, int userbuf)
89 int rc;
91 if (src < sclp.hsa_size) {
92 rc = memcpy_hsa(buf, src, csize, userbuf);
93 } else {
94 if (userbuf)
95 rc = copy_to_user_real((void __force __user *) buf,
96 (void *) src, csize);
97 else
98 rc = memcpy_real(buf, (void *) src, csize);
100 return rc ? rc : csize;
104 * Copy one page from kdump "oldmem"
106 * For the kdump reserved memory this functions performs a swap operation:
107 * - [OLDMEM_BASE - OLDMEM_BASE + OLDMEM_SIZE] is mapped to [0 - OLDMEM_SIZE].
108 * - [0 - OLDMEM_SIZE] is mapped to [OLDMEM_BASE - OLDMEM_BASE + OLDMEM_SIZE]
110 static ssize_t copy_oldmem_page_kdump(char *buf, size_t csize,
111 unsigned long src, int userbuf)
114 int rc;
116 if (src < OLDMEM_SIZE)
117 src += OLDMEM_BASE;
118 else if (src > OLDMEM_BASE &&
119 src < OLDMEM_BASE + OLDMEM_SIZE)
120 src -= OLDMEM_BASE;
121 if (userbuf)
122 rc = copy_to_user_real((void __force __user *) buf,
123 (void *) src, csize);
124 else
125 rc = copy_from_realmem(buf, (void *) src, csize);
126 return (rc == 0) ? rc : csize;
130 * Copy one page from "oldmem"
132 ssize_t copy_oldmem_page(unsigned long pfn, char *buf, size_t csize,
133 unsigned long offset, int userbuf)
135 unsigned long src;
137 if (!csize)
138 return 0;
139 src = (pfn << PAGE_SHIFT) + offset;
140 if (OLDMEM_BASE)
141 return copy_oldmem_page_kdump(buf, csize, src, userbuf);
142 else
143 return copy_oldmem_page_zfcpdump(buf, csize, src, userbuf);
147 * Remap "oldmem" for kdump
149 * For the kdump reserved memory this functions performs a swap operation:
150 * [0 - OLDMEM_SIZE] is mapped to [OLDMEM_BASE - OLDMEM_BASE + OLDMEM_SIZE]
152 static int remap_oldmem_pfn_range_kdump(struct vm_area_struct *vma,
153 unsigned long from, unsigned long pfn,
154 unsigned long size, pgprot_t prot)
156 unsigned long size_old;
157 int rc;
159 if (pfn < OLDMEM_SIZE >> PAGE_SHIFT) {
160 size_old = min(size, OLDMEM_SIZE - (pfn << PAGE_SHIFT));
161 rc = remap_pfn_range(vma, from,
162 pfn + (OLDMEM_BASE >> PAGE_SHIFT),
163 size_old, prot);
164 if (rc || size == size_old)
165 return rc;
166 size -= size_old;
167 from += size_old;
168 pfn += size_old >> PAGE_SHIFT;
170 return remap_pfn_range(vma, from, pfn, size, prot);
174 * Remap "oldmem" for zfcpdump
176 * We only map available memory above HSA size. Memory below HSA size
177 * is read on demand using the copy_oldmem_page() function.
179 static int remap_oldmem_pfn_range_zfcpdump(struct vm_area_struct *vma,
180 unsigned long from,
181 unsigned long pfn,
182 unsigned long size, pgprot_t prot)
184 unsigned long hsa_end = sclp.hsa_size;
185 unsigned long size_hsa;
187 if (pfn < hsa_end >> PAGE_SHIFT) {
188 size_hsa = min(size, hsa_end - (pfn << PAGE_SHIFT));
189 if (size == size_hsa)
190 return 0;
191 size -= size_hsa;
192 from += size_hsa;
193 pfn += size_hsa >> PAGE_SHIFT;
195 return remap_pfn_range(vma, from, pfn, size, prot);
199 * Remap "oldmem" for kdump or zfcpdump
201 int remap_oldmem_pfn_range(struct vm_area_struct *vma, unsigned long from,
202 unsigned long pfn, unsigned long size, pgprot_t prot)
204 if (OLDMEM_BASE)
205 return remap_oldmem_pfn_range_kdump(vma, from, pfn, size, prot);
206 else
207 return remap_oldmem_pfn_range_zfcpdump(vma, from, pfn, size,
208 prot);
212 * Copy memory from old kernel
214 int copy_from_oldmem(void *dest, void *src, size_t count)
216 unsigned long copied = 0;
217 int rc;
219 if (OLDMEM_BASE) {
220 if ((unsigned long) src < OLDMEM_SIZE) {
221 copied = min(count, OLDMEM_SIZE - (unsigned long) src);
222 rc = copy_from_realmem(dest, src + OLDMEM_BASE, copied);
223 if (rc)
224 return rc;
226 } else {
227 unsigned long hsa_end = sclp.hsa_size;
228 if ((unsigned long) src < hsa_end) {
229 copied = min(count, hsa_end - (unsigned long) src);
230 rc = memcpy_hsa(dest, (unsigned long) src, copied, 0);
231 if (rc)
232 return rc;
235 return copy_from_realmem(dest + copied, src + copied, count - copied);
239 * Alloc memory and panic in case of ENOMEM
241 static void *kzalloc_panic(int len)
243 void *rc;
245 rc = kzalloc(len, GFP_KERNEL);
246 if (!rc)
247 panic("s390 kdump kzalloc (%d) failed", len);
248 return rc;
252 * Initialize ELF note
254 static void *nt_init(void *buf, Elf64_Word type, void *desc, int d_len,
255 const char *name)
257 Elf64_Nhdr *note;
258 u64 len;
260 note = (Elf64_Nhdr *)buf;
261 note->n_namesz = strlen(name) + 1;
262 note->n_descsz = d_len;
263 note->n_type = type;
264 len = sizeof(Elf64_Nhdr);
266 memcpy(buf + len, name, note->n_namesz);
267 len = roundup(len + note->n_namesz, 4);
269 memcpy(buf + len, desc, note->n_descsz);
270 len = roundup(len + note->n_descsz, 4);
272 return PTR_ADD(buf, len);
276 * Initialize prstatus note
278 static void *nt_prstatus(void *ptr, struct save_area *sa)
280 struct elf_prstatus nt_prstatus;
281 static int cpu_nr = 1;
283 memset(&nt_prstatus, 0, sizeof(nt_prstatus));
284 memcpy(&nt_prstatus.pr_reg.gprs, sa->gp_regs, sizeof(sa->gp_regs));
285 memcpy(&nt_prstatus.pr_reg.psw, sa->psw, sizeof(sa->psw));
286 memcpy(&nt_prstatus.pr_reg.acrs, sa->acc_regs, sizeof(sa->acc_regs));
287 nt_prstatus.pr_pid = cpu_nr;
288 cpu_nr++;
290 return nt_init(ptr, NT_PRSTATUS, &nt_prstatus, sizeof(nt_prstatus),
291 "CORE");
295 * Initialize fpregset (floating point) note
297 static void *nt_fpregset(void *ptr, struct save_area *sa)
299 elf_fpregset_t nt_fpregset;
301 memset(&nt_fpregset, 0, sizeof(nt_fpregset));
302 memcpy(&nt_fpregset.fpc, &sa->fp_ctrl_reg, sizeof(sa->fp_ctrl_reg));
303 memcpy(&nt_fpregset.fprs, &sa->fp_regs, sizeof(sa->fp_regs));
305 return nt_init(ptr, NT_PRFPREG, &nt_fpregset, sizeof(nt_fpregset),
306 "CORE");
310 * Initialize timer note
312 static void *nt_s390_timer(void *ptr, struct save_area *sa)
314 return nt_init(ptr, NT_S390_TIMER, &sa->timer, sizeof(sa->timer),
315 KEXEC_CORE_NOTE_NAME);
319 * Initialize TOD clock comparator note
321 static void *nt_s390_tod_cmp(void *ptr, struct save_area *sa)
323 return nt_init(ptr, NT_S390_TODCMP, &sa->clk_cmp,
324 sizeof(sa->clk_cmp), KEXEC_CORE_NOTE_NAME);
328 * Initialize TOD programmable register note
330 static void *nt_s390_tod_preg(void *ptr, struct save_area *sa)
332 return nt_init(ptr, NT_S390_TODPREG, &sa->tod_reg,
333 sizeof(sa->tod_reg), KEXEC_CORE_NOTE_NAME);
337 * Initialize control register note
339 static void *nt_s390_ctrs(void *ptr, struct save_area *sa)
341 return nt_init(ptr, NT_S390_CTRS, &sa->ctrl_regs,
342 sizeof(sa->ctrl_regs), KEXEC_CORE_NOTE_NAME);
346 * Initialize prefix register note
348 static void *nt_s390_prefix(void *ptr, struct save_area *sa)
350 return nt_init(ptr, NT_S390_PREFIX, &sa->pref_reg,
351 sizeof(sa->pref_reg), KEXEC_CORE_NOTE_NAME);
355 * Initialize vxrs high note (full 128 bit VX registers 16-31)
357 static void *nt_s390_vx_high(void *ptr, __vector128 *vx_regs)
359 return nt_init(ptr, NT_S390_VXRS_HIGH, &vx_regs[16],
360 16 * sizeof(__vector128), KEXEC_CORE_NOTE_NAME);
364 * Initialize vxrs low note (lower halves of VX registers 0-15)
366 static void *nt_s390_vx_low(void *ptr, __vector128 *vx_regs)
368 Elf64_Nhdr *note;
369 u64 len;
370 int i;
372 note = (Elf64_Nhdr *)ptr;
373 note->n_namesz = strlen(KEXEC_CORE_NOTE_NAME) + 1;
374 note->n_descsz = 16 * 8;
375 note->n_type = NT_S390_VXRS_LOW;
376 len = sizeof(Elf64_Nhdr);
378 memcpy(ptr + len, KEXEC_CORE_NOTE_NAME, note->n_namesz);
379 len = roundup(len + note->n_namesz, 4);
381 ptr += len;
382 /* Copy lower halves of SIMD registers 0-15 */
383 for (i = 0; i < 16; i++) {
384 memcpy(ptr, &vx_regs[i].u[2], 8);
385 ptr += 8;
387 return ptr;
391 * Fill ELF notes for one CPU with save area registers
393 void *fill_cpu_elf_notes(void *ptr, struct save_area *sa, __vector128 *vx_regs)
395 ptr = nt_prstatus(ptr, sa);
396 ptr = nt_fpregset(ptr, sa);
397 ptr = nt_s390_timer(ptr, sa);
398 ptr = nt_s390_tod_cmp(ptr, sa);
399 ptr = nt_s390_tod_preg(ptr, sa);
400 ptr = nt_s390_ctrs(ptr, sa);
401 ptr = nt_s390_prefix(ptr, sa);
402 if (MACHINE_HAS_VX && vx_regs) {
403 ptr = nt_s390_vx_low(ptr, vx_regs);
404 ptr = nt_s390_vx_high(ptr, vx_regs);
406 return ptr;
410 * Initialize prpsinfo note (new kernel)
412 static void *nt_prpsinfo(void *ptr)
414 struct elf_prpsinfo prpsinfo;
416 memset(&prpsinfo, 0, sizeof(prpsinfo));
417 prpsinfo.pr_sname = 'R';
418 strcpy(prpsinfo.pr_fname, "vmlinux");
419 return nt_init(ptr, NT_PRPSINFO, &prpsinfo, sizeof(prpsinfo),
420 KEXEC_CORE_NOTE_NAME);
424 * Get vmcoreinfo using lowcore->vmcore_info (new kernel)
426 static void *get_vmcoreinfo_old(unsigned long *size)
428 char nt_name[11], *vmcoreinfo;
429 Elf64_Nhdr note;
430 void *addr;
432 if (copy_from_oldmem(&addr, &S390_lowcore.vmcore_info, sizeof(addr)))
433 return NULL;
434 memset(nt_name, 0, sizeof(nt_name));
435 if (copy_from_oldmem(&note, addr, sizeof(note)))
436 return NULL;
437 if (copy_from_oldmem(nt_name, addr + sizeof(note), sizeof(nt_name) - 1))
438 return NULL;
439 if (strcmp(nt_name, "VMCOREINFO") != 0)
440 return NULL;
441 vmcoreinfo = kzalloc_panic(note.n_descsz);
442 if (copy_from_oldmem(vmcoreinfo, addr + 24, note.n_descsz))
443 return NULL;
444 *size = note.n_descsz;
445 return vmcoreinfo;
449 * Initialize vmcoreinfo note (new kernel)
451 static void *nt_vmcoreinfo(void *ptr)
453 unsigned long size;
454 void *vmcoreinfo;
456 vmcoreinfo = os_info_old_entry(OS_INFO_VMCOREINFO, &size);
457 if (!vmcoreinfo)
458 vmcoreinfo = get_vmcoreinfo_old(&size);
459 if (!vmcoreinfo)
460 return ptr;
461 return nt_init(ptr, 0, vmcoreinfo, size, "VMCOREINFO");
465 * Initialize ELF header (new kernel)
467 static void *ehdr_init(Elf64_Ehdr *ehdr, int mem_chunk_cnt)
469 memset(ehdr, 0, sizeof(*ehdr));
470 memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
471 ehdr->e_ident[EI_CLASS] = ELFCLASS64;
472 ehdr->e_ident[EI_DATA] = ELFDATA2MSB;
473 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
474 memset(ehdr->e_ident + EI_PAD, 0, EI_NIDENT - EI_PAD);
475 ehdr->e_type = ET_CORE;
476 ehdr->e_machine = EM_S390;
477 ehdr->e_version = EV_CURRENT;
478 ehdr->e_phoff = sizeof(Elf64_Ehdr);
479 ehdr->e_ehsize = sizeof(Elf64_Ehdr);
480 ehdr->e_phentsize = sizeof(Elf64_Phdr);
481 ehdr->e_phnum = mem_chunk_cnt + 1;
482 return ehdr + 1;
486 * Return CPU count for ELF header (new kernel)
488 static int get_cpu_cnt(void)
490 int i, cpus = 0;
492 for (i = 0; i < dump_save_areas.count; i++) {
493 if (dump_save_areas.areas[i]->sa.pref_reg == 0)
494 continue;
495 cpus++;
497 return cpus;
501 * Return memory chunk count for ELF header (new kernel)
503 static int get_mem_chunk_cnt(void)
505 int cnt = 0;
506 u64 idx;
508 for_each_mem_range(idx, &memblock.physmem, &oldmem_type, NUMA_NO_NODE,
509 MEMBLOCK_NONE, NULL, NULL, NULL)
510 cnt++;
511 return cnt;
515 * Initialize ELF loads (new kernel)
517 static void loads_init(Elf64_Phdr *phdr, u64 loads_offset)
519 phys_addr_t start, end;
520 u64 idx;
522 for_each_mem_range(idx, &memblock.physmem, &oldmem_type, NUMA_NO_NODE,
523 MEMBLOCK_NONE, &start, &end, NULL) {
524 phdr->p_filesz = end - start;
525 phdr->p_type = PT_LOAD;
526 phdr->p_offset = start;
527 phdr->p_vaddr = start;
528 phdr->p_paddr = start;
529 phdr->p_memsz = end - start;
530 phdr->p_flags = PF_R | PF_W | PF_X;
531 phdr->p_align = PAGE_SIZE;
532 phdr++;
537 * Initialize notes (new kernel)
539 static void *notes_init(Elf64_Phdr *phdr, void *ptr, u64 notes_offset)
541 struct save_area_ext *sa_ext;
542 void *ptr_start = ptr;
543 int i;
545 ptr = nt_prpsinfo(ptr);
547 for (i = 0; i < dump_save_areas.count; i++) {
548 sa_ext = dump_save_areas.areas[i];
549 if (sa_ext->sa.pref_reg == 0)
550 continue;
551 ptr = fill_cpu_elf_notes(ptr, &sa_ext->sa, sa_ext->vx_regs);
553 ptr = nt_vmcoreinfo(ptr);
554 memset(phdr, 0, sizeof(*phdr));
555 phdr->p_type = PT_NOTE;
556 phdr->p_offset = notes_offset;
557 phdr->p_filesz = (unsigned long) PTR_SUB(ptr, ptr_start);
558 phdr->p_memsz = phdr->p_filesz;
559 return ptr;
563 * Create ELF core header (new kernel)
565 int elfcorehdr_alloc(unsigned long long *addr, unsigned long long *size)
567 Elf64_Phdr *phdr_notes, *phdr_loads;
568 int mem_chunk_cnt;
569 void *ptr, *hdr;
570 u32 alloc_size;
571 u64 hdr_off;
573 /* If we are not in kdump or zfcpdump mode return */
574 if (!OLDMEM_BASE && ipl_info.type != IPL_TYPE_FCP_DUMP)
575 return 0;
576 /* If elfcorehdr= has been passed via cmdline, we use that one */
577 if (elfcorehdr_addr != ELFCORE_ADDR_MAX)
578 return 0;
579 /* If we cannot get HSA size for zfcpdump return error */
580 if (ipl_info.type == IPL_TYPE_FCP_DUMP && !sclp.hsa_size)
581 return -ENODEV;
583 /* For kdump, exclude previous crashkernel memory */
584 if (OLDMEM_BASE) {
585 oldmem_region.base = OLDMEM_BASE;
586 oldmem_region.size = OLDMEM_SIZE;
587 oldmem_type.total_size = OLDMEM_SIZE;
590 mem_chunk_cnt = get_mem_chunk_cnt();
592 alloc_size = 0x1000 + get_cpu_cnt() * 0x4a0 +
593 mem_chunk_cnt * sizeof(Elf64_Phdr);
594 hdr = kzalloc_panic(alloc_size);
595 /* Init elf header */
596 ptr = ehdr_init(hdr, mem_chunk_cnt);
597 /* Init program headers */
598 phdr_notes = ptr;
599 ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr));
600 phdr_loads = ptr;
601 ptr = PTR_ADD(ptr, sizeof(Elf64_Phdr) * mem_chunk_cnt);
602 /* Init notes */
603 hdr_off = PTR_DIFF(ptr, hdr);
604 ptr = notes_init(phdr_notes, ptr, ((unsigned long) hdr) + hdr_off);
605 /* Init loads */
606 hdr_off = PTR_DIFF(ptr, hdr);
607 loads_init(phdr_loads, hdr_off);
608 *addr = (unsigned long long) hdr;
609 elfcorehdr_newmem = hdr;
610 *size = (unsigned long long) hdr_off;
611 BUG_ON(elfcorehdr_size > alloc_size);
612 return 0;
616 * Free ELF core header (new kernel)
618 void elfcorehdr_free(unsigned long long addr)
620 if (!elfcorehdr_newmem)
621 return;
622 kfree((void *)(unsigned long)addr);
626 * Read from ELF header
628 ssize_t elfcorehdr_read(char *buf, size_t count, u64 *ppos)
630 void *src = (void *)(unsigned long)*ppos;
632 src = elfcorehdr_newmem ? src : src - OLDMEM_BASE;
633 memcpy(buf, src, count);
634 *ppos += count;
635 return count;
639 * Read from ELF notes data
641 ssize_t elfcorehdr_read_notes(char *buf, size_t count, u64 *ppos)
643 void *src = (void *)(unsigned long)*ppos;
644 int rc;
646 if (elfcorehdr_newmem) {
647 memcpy(buf, src, count);
648 } else {
649 rc = copy_from_oldmem(buf, src, count);
650 if (rc)
651 return rc;
653 *ppos += count;
654 return count;