^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) 2017 Linaro Limited
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/crash_dump.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/memblock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <asm/memory.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * copy_oldmem_page() - copy one page from old kernel memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * @pfn: page frame number to be copied
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * @buf: buffer where the copied page is placed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * @csize: number of bytes to copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * @offset: offset in bytes into the page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * @userbuf: if set, @buf is in a user address space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * This function copies one page from old kernel memory into buffer pointed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * @buf. If @buf is in userspace, set @userbuf to %1. Returns number of bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * copied or negative error in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) ssize_t copy_oldmem_page(unsigned long pfn, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) size_t csize, unsigned long offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) int userbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) void *vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (!csize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) vaddr = memremap(__pfn_to_phys(pfn), PAGE_SIZE, MEMREMAP_WB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (!vaddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (userbuf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (copy_to_user((char __user *)buf, vaddr + offset, csize)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) memunmap(vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) memcpy(buf, vaddr + offset, csize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) memunmap(vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return csize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * elfcorehdr_read - read from ELF core header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * @buf: buffer where the data is placed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * @count: number of bytes to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * @ppos: address in the memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * This function reads @count bytes from elf core header which exists
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * on crash dump kernel's memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) ssize_t elfcorehdr_read(char *buf, size_t count, u64 *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) memcpy(buf, phys_to_virt((phys_addr_t)*ppos), count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) *ppos += count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }