xref: /linux/arch/x86/kernel/crash_dump_64.c (revision a4eb44a6435d6d8f9e642407a4a06f65eb90ca04)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *	Memory preserving reboot related code.
4  *
5  *	Created by: Hariprasad Nellitheertha (hari@in.ibm.com)
6  *	Copyright (C) IBM Corporation, 2004. All rights reserved
7  */
8 
9 #include <linux/errno.h>
10 #include <linux/crash_dump.h>
11 #include <linux/uaccess.h>
12 #include <linux/io.h>
13 #include <linux/cc_platform.h>
14 
15 static ssize_t __copy_oldmem_page(unsigned long pfn, char *buf, size_t csize,
16 				  unsigned long offset, int userbuf,
17 				  bool encrypted)
18 {
19 	void  *vaddr;
20 
21 	if (!csize)
22 		return 0;
23 
24 	if (encrypted)
25 		vaddr = (__force void *)ioremap_encrypted(pfn << PAGE_SHIFT, PAGE_SIZE);
26 	else
27 		vaddr = (__force void *)ioremap_cache(pfn << PAGE_SHIFT, PAGE_SIZE);
28 
29 	if (!vaddr)
30 		return -ENOMEM;
31 
32 	if (userbuf) {
33 		if (copy_to_user((void __user *)buf, vaddr + offset, csize)) {
34 			iounmap((void __iomem *)vaddr);
35 			return -EFAULT;
36 		}
37 	} else
38 		memcpy(buf, vaddr + offset, csize);
39 
40 	set_iounmap_nonlazy();
41 	iounmap((void __iomem *)vaddr);
42 	return csize;
43 }
44 
45 /**
46  * copy_oldmem_page - copy one page of memory
47  * @pfn: page frame number to be copied
48  * @buf: target memory address for the copy; this can be in kernel address
49  *	space or user address space (see @userbuf)
50  * @csize: number of bytes to copy
51  * @offset: offset in bytes into the page (based on pfn) to begin the copy
52  * @userbuf: if set, @buf is in user address space, use copy_to_user(),
53  *	otherwise @buf is in kernel address space, use memcpy().
54  *
55  * Copy a page from the old kernel's memory. For this page, there is no pte
56  * mapped in the current kernel. We stitch up a pte, similar to kmap_atomic.
57  */
58 ssize_t copy_oldmem_page(unsigned long pfn, char *buf, size_t csize,
59 			 unsigned long offset, int userbuf)
60 {
61 	return __copy_oldmem_page(pfn, buf, csize, offset, userbuf, false);
62 }
63 
64 /**
65  * copy_oldmem_page_encrypted - same as copy_oldmem_page() above but ioremap the
66  * memory with the encryption mask set to accommodate kdump on SME-enabled
67  * machines.
68  */
69 ssize_t copy_oldmem_page_encrypted(unsigned long pfn, char *buf, size_t csize,
70 				   unsigned long offset, int userbuf)
71 {
72 	return __copy_oldmem_page(pfn, buf, csize, offset, userbuf, true);
73 }
74 
75 ssize_t elfcorehdr_read(char *buf, size_t count, u64 *ppos)
76 {
77 	return read_from_oldmem(buf, count, ppos, 0,
78 				cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT));
79 }
80