Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Routines for doing kexec-based kdump.
0004  *
0005  * Copyright (C) 2005, IBM Corp.
0006  *
0007  * Created by: Michael Ellerman
0008  */
0009 
0010 #undef DEBUG
0011 
0012 #include <linux/crash_dump.h>
0013 #include <linux/io.h>
0014 #include <linux/memblock.h>
0015 #include <linux/of.h>
0016 #include <asm/code-patching.h>
0017 #include <asm/kdump.h>
0018 #include <asm/firmware.h>
0019 #include <linux/uio.h>
0020 #include <asm/rtas.h>
0021 #include <asm/inst.h>
0022 
0023 #ifdef DEBUG
0024 #include <asm/udbg.h>
0025 #define DBG(fmt...) udbg_printf(fmt)
0026 #else
0027 #define DBG(fmt...)
0028 #endif
0029 
0030 #ifndef CONFIG_NONSTATIC_KERNEL
0031 void __init reserve_kdump_trampoline(void)
0032 {
0033     memblock_reserve(0, KDUMP_RESERVE_LIMIT);
0034 }
0035 
0036 static void __init create_trampoline(unsigned long addr)
0037 {
0038     u32 *p = (u32 *)addr;
0039 
0040     /* The maximum range of a single instruction branch, is the current
0041      * instruction's address + (32 MB - 4) bytes. For the trampoline we
0042      * need to branch to current address + 32 MB. So we insert a nop at
0043      * the trampoline address, then the next instruction (+ 4 bytes)
0044      * does a branch to (32 MB - 4). The net effect is that when we
0045      * branch to "addr" we jump to ("addr" + 32 MB). Although it requires
0046      * two instructions it doesn't require any registers.
0047      */
0048     patch_instruction(p, ppc_inst(PPC_RAW_NOP()));
0049     patch_branch(p + 1, addr + PHYSICAL_START, 0);
0050 }
0051 
0052 void __init setup_kdump_trampoline(void)
0053 {
0054     unsigned long i;
0055 
0056     DBG(" -> setup_kdump_trampoline()\n");
0057 
0058     for (i = KDUMP_TRAMPOLINE_START; i < KDUMP_TRAMPOLINE_END; i += 8) {
0059         create_trampoline(i);
0060     }
0061 
0062 #ifdef CONFIG_PPC_PSERIES
0063     create_trampoline(__pa(system_reset_fwnmi) - PHYSICAL_START);
0064     create_trampoline(__pa(machine_check_fwnmi) - PHYSICAL_START);
0065 #endif /* CONFIG_PPC_PSERIES */
0066 
0067     DBG(" <- setup_kdump_trampoline()\n");
0068 }
0069 #endif /* CONFIG_NONSTATIC_KERNEL */
0070 
0071 ssize_t copy_oldmem_page(struct iov_iter *iter, unsigned long pfn,
0072             size_t csize, unsigned long offset)
0073 {
0074     void  *vaddr;
0075     phys_addr_t paddr;
0076 
0077     if (!csize)
0078         return 0;
0079 
0080     csize = min_t(size_t, csize, PAGE_SIZE);
0081     paddr = pfn << PAGE_SHIFT;
0082 
0083     if (memblock_is_region_memory(paddr, csize)) {
0084         vaddr = __va(paddr);
0085         csize = copy_to_iter(vaddr + offset, csize, iter);
0086     } else {
0087         vaddr = ioremap_cache(paddr, PAGE_SIZE);
0088         csize = copy_to_iter(vaddr + offset, csize, iter);
0089         iounmap(vaddr);
0090     }
0091 
0092     return csize;
0093 }
0094 
0095 #ifdef CONFIG_PPC_RTAS
0096 /*
0097  * The crashkernel region will almost always overlap the RTAS region, so
0098  * we have to be careful when shrinking the crashkernel region.
0099  */
0100 void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
0101 {
0102     unsigned long addr;
0103     const __be32 *basep, *sizep;
0104     unsigned int rtas_start = 0, rtas_end = 0;
0105 
0106     basep = of_get_property(rtas.dev, "linux,rtas-base", NULL);
0107     sizep = of_get_property(rtas.dev, "rtas-size", NULL);
0108 
0109     if (basep && sizep) {
0110         rtas_start = be32_to_cpup(basep);
0111         rtas_end = rtas_start + be32_to_cpup(sizep);
0112     }
0113 
0114     for (addr = begin; addr < end; addr += PAGE_SIZE) {
0115         /* Does this page overlap with the RTAS region? */
0116         if (addr <= rtas_end && ((addr + PAGE_SIZE) > rtas_start))
0117             continue;
0118 
0119         free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
0120     }
0121 }
0122 #endif