xref: /openbmc/linux/arch/powerpc/kernel/crash_dump.c (revision a29e30ef)
10cc4746cSMichael Ellerman /*
20cc4746cSMichael Ellerman  * Routines for doing kexec-based kdump.
30cc4746cSMichael Ellerman  *
40cc4746cSMichael Ellerman  * Copyright (C) 2005, IBM Corp.
50cc4746cSMichael Ellerman  *
60cc4746cSMichael Ellerman  * Created by: Michael Ellerman
70cc4746cSMichael Ellerman  *
80cc4746cSMichael Ellerman  * This source code is licensed under the GNU General Public License,
90cc4746cSMichael Ellerman  * Version 2.  See the file COPYING for more details.
100cc4746cSMichael Ellerman  */
110cc4746cSMichael Ellerman 
120cc4746cSMichael Ellerman #undef DEBUG
130cc4746cSMichael Ellerman 
14cc532915SMichael Ellerman #include <linux/crash_dump.h>
15cc532915SMichael Ellerman #include <linux/bootmem.h>
1695f72d1eSYinghai Lu #include <linux/memblock.h>
17aaddd3eaSMichael Ellerman #include <asm/code-patching.h>
180cc4746cSMichael Ellerman #include <asm/kdump.h>
19d9b2b2a2SDavid S. Miller #include <asm/prom.h>
200cc4746cSMichael Ellerman #include <asm/firmware.h>
2154c32021SMichael Ellerman #include <asm/uaccess.h>
22d72e063bSAnton Blanchard #include <asm/rtas.h>
230cc4746cSMichael Ellerman 
240cc4746cSMichael Ellerman #ifdef DEBUG
250cc4746cSMichael Ellerman #include <asm/udbg.h>
260cc4746cSMichael Ellerman #define DBG(fmt...) udbg_printf(fmt)
270cc4746cSMichael Ellerman #else
280cc4746cSMichael Ellerman #define DBG(fmt...)
290cc4746cSMichael Ellerman #endif
300cc4746cSMichael Ellerman 
310f890c8dSSuzuki Poulose #ifndef CONFIG_NONSTATIC_KERNEL
32d56c3aaaSStephen Rothwell void __init reserve_kdump_trampoline(void)
3347310413SMichael Ellerman {
3495f72d1eSYinghai Lu 	memblock_reserve(0, KDUMP_RESERVE_LIMIT);
3547310413SMichael Ellerman }
3647310413SMichael Ellerman 
370cc4746cSMichael Ellerman static void __init create_trampoline(unsigned long addr)
380cc4746cSMichael Ellerman {
39e7a57273SMichael Ellerman 	unsigned int *p = (unsigned int *)addr;
40e7a57273SMichael Ellerman 
410cc4746cSMichael Ellerman 	/* The maximum range of a single instruction branch, is the current
420cc4746cSMichael Ellerman 	 * instruction's address + (32 MB - 4) bytes. For the trampoline we
430cc4746cSMichael Ellerman 	 * need to branch to current address + 32 MB. So we insert a nop at
440cc4746cSMichael Ellerman 	 * the trampoline address, then the next instruction (+ 4 bytes)
450cc4746cSMichael Ellerman 	 * does a branch to (32 MB - 4). The net effect is that when we
460cc4746cSMichael Ellerman 	 * branch to "addr" we jump to ("addr" + 32 MB). Although it requires
470cc4746cSMichael Ellerman 	 * two instructions it doesn't require any registers.
480cc4746cSMichael Ellerman 	 */
4916c57b36SKumar Gala 	patch_instruction(p, PPC_INST_NOP);
50e7a57273SMichael Ellerman 	patch_branch(++p, addr + PHYSICAL_START, 0);
510cc4746cSMichael Ellerman }
520cc4746cSMichael Ellerman 
5347310413SMichael Ellerman void __init setup_kdump_trampoline(void)
540cc4746cSMichael Ellerman {
550cc4746cSMichael Ellerman 	unsigned long i;
560cc4746cSMichael Ellerman 
5747310413SMichael Ellerman 	DBG(" -> setup_kdump_trampoline()\n");
580cc4746cSMichael Ellerman 
590cc4746cSMichael Ellerman 	for (i = KDUMP_TRAMPOLINE_START; i < KDUMP_TRAMPOLINE_END; i += 8) {
600cc4746cSMichael Ellerman 		create_trampoline(i);
610cc4746cSMichael Ellerman 	}
620cc4746cSMichael Ellerman 
639e4859efSStephen Rothwell #ifdef CONFIG_PPC_PSERIES
640cc4746cSMichael Ellerman 	create_trampoline(__pa(system_reset_fwnmi) - PHYSICAL_START);
650cc4746cSMichael Ellerman 	create_trampoline(__pa(machine_check_fwnmi) - PHYSICAL_START);
669e4859efSStephen Rothwell #endif /* CONFIG_PPC_PSERIES */
670cc4746cSMichael Ellerman 
6847310413SMichael Ellerman 	DBG(" <- setup_kdump_trampoline()\n");
690cc4746cSMichael Ellerman }
700f890c8dSSuzuki Poulose #endif /* CONFIG_NONSTATIC_KERNEL */
71cc532915SMichael Ellerman 
727230ced4SMichael Ellerman static size_t copy_oldmem_vaddr(void *vaddr, char *buf, size_t csize,
737230ced4SMichael Ellerman                                unsigned long offset, int userbuf)
747230ced4SMichael Ellerman {
757230ced4SMichael Ellerman 	if (userbuf) {
767230ced4SMichael Ellerman 		if (copy_to_user((char __user *)buf, (vaddr + offset), csize))
777230ced4SMichael Ellerman 			return -EFAULT;
787230ced4SMichael Ellerman 	} else
797230ced4SMichael Ellerman 		memcpy(buf, (vaddr + offset), csize);
807230ced4SMichael Ellerman 
817230ced4SMichael Ellerman 	return csize;
827230ced4SMichael Ellerman }
837230ced4SMichael Ellerman 
8440681b95SMichael Ellerman /**
8554c32021SMichael Ellerman  * copy_oldmem_page - copy one page from "oldmem"
8654c32021SMichael Ellerman  * @pfn: page frame number to be copied
8754c32021SMichael Ellerman  * @buf: target memory address for the copy; this can be in kernel address
8854c32021SMichael Ellerman  *      space or user address space (see @userbuf)
8954c32021SMichael Ellerman  * @csize: number of bytes to copy
9054c32021SMichael Ellerman  * @offset: offset in bytes into the page (based on pfn) to begin the copy
9154c32021SMichael Ellerman  * @userbuf: if set, @buf is in user address space, use copy_to_user(),
9254c32021SMichael Ellerman  *      otherwise @buf is in kernel address space, use memcpy().
9354c32021SMichael Ellerman  *
9454c32021SMichael Ellerman  * Copy a page from "oldmem". For this page, there is no pte mapped
9554c32021SMichael Ellerman  * in the current kernel. We stitch up a pte, similar to kmap_atomic.
9654c32021SMichael Ellerman  */
9754c32021SMichael Ellerman ssize_t copy_oldmem_page(unsigned long pfn, char *buf,
9854c32021SMichael Ellerman 			size_t csize, unsigned long offset, int userbuf)
9954c32021SMichael Ellerman {
10054c32021SMichael Ellerman 	void  *vaddr;
10154c32021SMichael Ellerman 
10254c32021SMichael Ellerman 	if (!csize)
10354c32021SMichael Ellerman 		return 0;
10454c32021SMichael Ellerman 
105bbc8e30fSMatthew McClintock 	csize = min_t(size_t, csize, PAGE_SIZE);
1067230ced4SMichael Ellerman 
107bbc8e30fSMatthew McClintock 	if ((min_low_pfn < pfn) && (pfn < max_pfn)) {
1087230ced4SMichael Ellerman 		vaddr = __va(pfn << PAGE_SHIFT);
1097230ced4SMichael Ellerman 		csize = copy_oldmem_vaddr(vaddr, buf, csize, offset, userbuf);
1107230ced4SMichael Ellerman 	} else {
11154c32021SMichael Ellerman 		vaddr = __ioremap(pfn << PAGE_SHIFT, PAGE_SIZE, 0);
1127230ced4SMichael Ellerman 		csize = copy_oldmem_vaddr(vaddr, buf, csize, offset, userbuf);
11354c32021SMichael Ellerman 		iounmap(vaddr);
11454c32021SMichael Ellerman 	}
11554c32021SMichael Ellerman 
11654c32021SMichael Ellerman 	return csize;
11754c32021SMichael Ellerman }
118d72e063bSAnton Blanchard 
119d72e063bSAnton Blanchard #ifdef CONFIG_PPC_RTAS
120d72e063bSAnton Blanchard /*
121d72e063bSAnton Blanchard  * The crashkernel region will almost always overlap the RTAS region, so
122d72e063bSAnton Blanchard  * we have to be careful when shrinking the crashkernel region.
123d72e063bSAnton Blanchard  */
124d72e063bSAnton Blanchard void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
125d72e063bSAnton Blanchard {
126d72e063bSAnton Blanchard 	unsigned long addr;
127a29e30efSAnton Blanchard 	const __be32 *basep, *sizep;
128d72e063bSAnton Blanchard 	unsigned int rtas_start = 0, rtas_end = 0;
129d72e063bSAnton Blanchard 
130d72e063bSAnton Blanchard 	basep = of_get_property(rtas.dev, "linux,rtas-base", NULL);
131d72e063bSAnton Blanchard 	sizep = of_get_property(rtas.dev, "rtas-size", NULL);
132d72e063bSAnton Blanchard 
133d72e063bSAnton Blanchard 	if (basep && sizep) {
134a29e30efSAnton Blanchard 		rtas_start = be32_to_cpup(basep);
135a29e30efSAnton Blanchard 		rtas_end = rtas_start + be32_to_cpup(sizep);
136d72e063bSAnton Blanchard 	}
137d72e063bSAnton Blanchard 
138d72e063bSAnton Blanchard 	for (addr = begin; addr < end; addr += PAGE_SIZE) {
139d72e063bSAnton Blanchard 		/* Does this page overlap with the RTAS region? */
140d72e063bSAnton Blanchard 		if (addr <= rtas_end && ((addr + PAGE_SIZE) > rtas_start))
141d72e063bSAnton Blanchard 			continue;
142d72e063bSAnton Blanchard 
1435d585e5cSJiang Liu 		free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
144d72e063bSAnton Blanchard 	}
145d72e063bSAnton Blanchard }
146d72e063bSAnton Blanchard #endif
147