^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Routines for doing kexec-based kdump.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2005, IBM Corp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Created by: Michael Ellerman
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #undef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/crash_dump.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/memblock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <asm/code-patching.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <asm/kdump.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <asm/prom.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <asm/firmware.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <asm/rtas.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <asm/inst.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #ifdef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <asm/udbg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #define DBG(fmt...) udbg_printf(fmt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define DBG(fmt...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #ifndef CONFIG_NONSTATIC_KERNEL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) void __init reserve_kdump_trampoline(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) memblock_reserve(0, KDUMP_RESERVE_LIMIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static void __init create_trampoline(unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct ppc_inst *p = (struct ppc_inst *)addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /* The maximum range of a single instruction branch, is the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * instruction's address + (32 MB - 4) bytes. For the trampoline we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * need to branch to current address + 32 MB. So we insert a nop at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * the trampoline address, then the next instruction (+ 4 bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * does a branch to (32 MB - 4). The net effect is that when we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * branch to "addr" we jump to ("addr" + 32 MB). Although it requires
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * two instructions it doesn't require any registers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) patch_instruction(p, ppc_inst(PPC_INST_NOP));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) patch_branch((void *)p + 4, addr + PHYSICAL_START, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) void __init setup_kdump_trampoline(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) unsigned long i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) DBG(" -> setup_kdump_trampoline()\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) for (i = KDUMP_TRAMPOLINE_START; i < KDUMP_TRAMPOLINE_END; i += 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) create_trampoline(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #ifdef CONFIG_PPC_PSERIES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) create_trampoline(__pa(system_reset_fwnmi) - PHYSICAL_START);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) create_trampoline(__pa(machine_check_fwnmi) - PHYSICAL_START);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #endif /* CONFIG_PPC_PSERIES */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) DBG(" <- setup_kdump_trampoline()\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) #endif /* CONFIG_NONSTATIC_KERNEL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static size_t copy_oldmem_vaddr(void *vaddr, char *buf, size_t csize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) unsigned long offset, int userbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (userbuf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (copy_to_user((char __user *)buf, (vaddr + offset), csize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) memcpy(buf, (vaddr + offset), csize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return csize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * copy_oldmem_page - copy one page from "oldmem"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * @pfn: page frame number to be copied
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * @buf: target memory address for the copy; this can be in kernel address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * space or user address space (see @userbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * @csize: number of bytes to copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * @offset: offset in bytes into the page (based on pfn) to begin the copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * @userbuf: if set, @buf is in user address space, use copy_to_user(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * otherwise @buf is in kernel address space, use memcpy().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * Copy a page from "oldmem". For this page, there is no pte mapped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * in the current kernel. We stitch up a pte, similar to kmap_atomic.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) ssize_t copy_oldmem_page(unsigned long pfn, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) size_t csize, unsigned long offset, int userbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) void *vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) phys_addr_t paddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (!csize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) csize = min_t(size_t, csize, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) paddr = pfn << PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (memblock_is_region_memory(paddr, csize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) vaddr = __va(paddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) csize = copy_oldmem_vaddr(vaddr, buf, csize, offset, userbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) vaddr = ioremap_cache(paddr, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) csize = copy_oldmem_vaddr(vaddr, buf, csize, offset, userbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) iounmap(vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return csize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) #ifdef CONFIG_PPC_RTAS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * The crashkernel region will almost always overlap the RTAS region, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * we have to be careful when shrinking the crashkernel region.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) unsigned long addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) const __be32 *basep, *sizep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) unsigned int rtas_start = 0, rtas_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) basep = of_get_property(rtas.dev, "linux,rtas-base", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) sizep = of_get_property(rtas.dev, "rtas-size", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (basep && sizep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) rtas_start = be32_to_cpup(basep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) rtas_end = rtas_start + be32_to_cpup(sizep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) for (addr = begin; addr < end; addr += PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) /* Does this page overlap with the RTAS region? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (addr <= rtas_end && ((addr + PAGE_SIZE) > rtas_start))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) free_reserved_page(pfn_to_page(addr >> PAGE_SHIFT));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) #endif