xref: /openbmc/linux/arch/riscv/kernel/crash_dump.c (revision 647d41d3)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * This code comes from arch/arm64/kernel/crash_dump.c
4  * Created by: AKASHI Takahiro <takahiro.akashi@linaro.org>
5  * Copyright (C) 2017 Linaro Limited
6  */
7 
8 #include <linux/crash_dump.h>
9 #include <linux/io.h>
10 
11 /**
12  * copy_oldmem_page() - copy one page from old kernel memory
13  * @pfn: page frame number to be copied
14  * @buf: buffer where the copied page is placed
15  * @csize: number of bytes to copy
16  * @offset: offset in bytes into the page
17  * @userbuf: if set, @buf is in a user address space
18  *
19  * This function copies one page from old kernel memory into buffer pointed by
20  * @buf. If @buf is in userspace, set @userbuf to %1. Returns number of bytes
21  * copied or negative error in case of failure.
22  */
23 ssize_t copy_oldmem_page(unsigned long pfn, char *buf,
24 			 size_t csize, unsigned long offset,
25 			 int userbuf)
26 {
27 	void *vaddr;
28 
29 	if (!csize)
30 		return 0;
31 
32 	vaddr = memremap(__pfn_to_phys(pfn), PAGE_SIZE, MEMREMAP_WB);
33 	if (!vaddr)
34 		return -ENOMEM;
35 
36 	if (userbuf) {
37 		if (copy_to_user((char __user *)buf, vaddr + offset, csize)) {
38 			memunmap(vaddr);
39 			return -EFAULT;
40 		}
41 	} else
42 		memcpy(buf, vaddr + offset, csize);
43 
44 	memunmap(vaddr);
45 	return csize;
46 }
47