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 <linux/memblock.h>
17 #include <asm/os_info.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
= {
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
;
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
)
62 if (!is_vmalloc_or_module_addr(dest
))
63 return memcpy_real(dest
, src
, count
);
65 size
= min(count
, PAGE_SIZE
- (__pa(dest
) & ~PAGE_MASK
));
66 if (memcpy_real(load_real_addr(dest
), src
, size
))
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
)
91 if (src
< sclp
.hsa_size
) {
92 rc
= memcpy_hsa(buf
, src
, csize
, userbuf
);
95 rc
= copy_to_user_real((void __force __user
*) buf
,
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
)
116 if (src
< OLDMEM_SIZE
)
118 else if (src
> OLDMEM_BASE
&&
119 src
< OLDMEM_BASE
+ OLDMEM_SIZE
)
122 rc
= copy_to_user_real((void __force __user
*) buf
,
123 (void *) src
, csize
);
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
)
139 src
= (pfn
<< PAGE_SHIFT
) + offset
;
141 return copy_oldmem_page_kdump(buf
, csize
, src
, userbuf
);
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
;
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
),
164 if (rc
|| size
== 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
,
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
)
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
)
205 return remap_oldmem_pfn_range_kdump(vma
, from
, pfn
, size
, prot
);
207 return remap_oldmem_pfn_range_zfcpdump(vma
, from
, pfn
, size
,
212 * Copy memory from old kernel
214 int copy_from_oldmem(void *dest
, void *src
, size_t count
)
216 unsigned long copied
= 0;
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
);
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);
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
)
245 rc
= kzalloc(len
, GFP_KERNEL
);
247 panic("s390 kdump kzalloc (%d) failed", len
);
252 * Initialize ELF note
254 static void *nt_init(void *buf
, Elf64_Word type
, void *desc
, int d_len
,
260 note
= (Elf64_Nhdr
*)buf
;
261 note
->n_namesz
= strlen(name
) + 1;
262 note
->n_descsz
= d_len
;
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
;
290 return nt_init(ptr
, NT_PRSTATUS
, &nt_prstatus
, sizeof(nt_prstatus
),
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
),
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
)
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);
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);
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
);
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
;
432 if (copy_from_oldmem(&addr
, &S390_lowcore
.vmcore_info
, sizeof(addr
)))
434 memset(nt_name
, 0, sizeof(nt_name
));
435 if (copy_from_oldmem(¬e
, addr
, sizeof(note
)))
437 if (copy_from_oldmem(nt_name
, addr
+ sizeof(note
), sizeof(nt_name
) - 1))
439 if (strcmp(nt_name
, "VMCOREINFO") != 0)
441 vmcoreinfo
= kzalloc_panic(note
.n_descsz
);
442 if (copy_from_oldmem(vmcoreinfo
, addr
+ 24, note
.n_descsz
))
444 *size
= note
.n_descsz
;
449 * Initialize vmcoreinfo note (new kernel)
451 static void *nt_vmcoreinfo(void *ptr
)
456 vmcoreinfo
= os_info_old_entry(OS_INFO_VMCOREINFO
, &size
);
458 vmcoreinfo
= get_vmcoreinfo_old(&size
);
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;
486 * Return CPU count for ELF header (new kernel)
488 static int get_cpu_cnt(void)
492 for (i
= 0; i
< dump_save_areas
.count
; i
++) {
493 if (dump_save_areas
.areas
[i
]->sa
.pref_reg
== 0)
501 * Return memory chunk count for ELF header (new kernel)
503 static int get_mem_chunk_cnt(void)
508 for_each_mem_range(idx
, &memblock
.physmem
, &oldmem_type
, NUMA_NO_NODE
,
509 MEMBLOCK_NONE
, NULL
, NULL
, NULL
)
515 * Initialize ELF loads (new kernel)
517 static void loads_init(Elf64_Phdr
*phdr
, u64 loads_offset
)
519 phys_addr_t start
, end
;
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
;
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
;
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)
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
;
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
;
573 /* If we are not in kdump or zfcpdump mode return */
574 if (!OLDMEM_BASE
&& ipl_info
.type
!= IPL_TYPE_FCP_DUMP
)
576 /* If elfcorehdr= has been passed via cmdline, we use that one */
577 if (elfcorehdr_addr
!= ELFCORE_ADDR_MAX
)
579 /* If we cannot get HSA size for zfcpdump return error */
580 if (ipl_info
.type
== IPL_TYPE_FCP_DUMP
&& !sclp
.hsa_size
)
583 /* For kdump, exclude previous crashkernel memory */
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 */
599 ptr
= PTR_ADD(ptr
, sizeof(Elf64_Phdr
));
601 ptr
= PTR_ADD(ptr
, sizeof(Elf64_Phdr
) * mem_chunk_cnt
);
603 hdr_off
= PTR_DIFF(ptr
, hdr
);
604 ptr
= notes_init(phdr_notes
, ptr
, ((unsigned long) hdr
) + hdr_off
);
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
);
616 * Free ELF core header (new kernel)
618 void elfcorehdr_free(unsigned long long addr
)
620 if (!elfcorehdr_newmem
)
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
);
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
;
646 if (elfcorehdr_newmem
) {
647 memcpy(buf
, src
, count
);
649 rc
= copy_from_oldmem(buf
, src
, count
);