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