blob: a7f617a3981d451c002317ac9bb64ab3baeaaa27 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Vivek Goyal60e64d42005-06-25 14:58:19 -07002/*
Dave Jones835c34a2007-10-12 21:10:53 -04003 * Memory preserving reboot related code.
Vivek Goyal60e64d42005-06-25 14:58:19 -07004 *
5 * Created by: Hariprasad Nellitheertha (hari@in.ibm.com)
6 * Copyright (C) IBM Corporation, 2004. All rights reserved
7 */
8
Vivek Goyal60e64d42005-06-25 14:58:19 -07009#include <linux/errno.h>
Vivek Goyal60e64d42005-06-25 14:58:19 -070010#include <linux/crash_dump.h>
Gustavo F. Padovan08aadf02008-07-29 02:48:53 -030011#include <linux/uaccess.h>
12#include <linux/io.h>
Tom Lendacky4d96f912021-09-08 17:58:37 -050013#include <linux/cc_platform.h>
Vivek Goyal60e64d42005-06-25 14:58:19 -070014
Lianbo Jiang992b6492018-09-30 16:37:41 +080015static 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
Randy Dunlape77e17162005-07-27 11:45:11 -070045/**
Lianbo Jiang992b6492018-09-30 16:37:41 +080046 * copy_oldmem_page - copy one page of memory
Randy Dunlape77e17162005-07-27 11:45:11 -070047 * @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 *
Lianbo Jiang992b6492018-09-30 16:37:41 +080055 * 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.
Vivek Goyal60e64d42005-06-25 14:58:19 -070057 */
Lianbo Jiang992b6492018-09-30 16:37:41 +080058ssize_t copy_oldmem_page(unsigned long pfn, char *buf, size_t csize,
59 unsigned long offset, int userbuf)
Vivek Goyal60e64d42005-06-25 14:58:19 -070060{
Lianbo Jiang992b6492018-09-30 16:37:41 +080061 return __copy_oldmem_page(pfn, buf, csize, offset, userbuf, false);
62}
Vivek Goyal60e64d42005-06-25 14:58:19 -070063
Lianbo Jiang992b6492018-09-30 16:37:41 +080064/**
65 * copy_oldmem_page_encrypted - same as copy_oldmem_page() above but ioremap the
Ingo Molnara97673a2018-12-03 10:47:34 +010066 * memory with the encryption mask set to accommodate kdump on SME-enabled
Lianbo Jiang992b6492018-09-30 16:37:41 +080067 * machines.
68 */
69ssize_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);
Vivek Goyal60e64d42005-06-25 14:58:19 -070073}
Thiago Jung Bauermannae7eb822019-08-06 01:49:18 -030074
75ssize_t elfcorehdr_read(char *buf, size_t count, u64 *ppos)
76{
Tom Lendacky4d96f912021-09-08 17:58:37 -050077 return read_from_oldmem(buf, count, ppos, 0,
78 cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT));
Thiago Jung Bauermannae7eb822019-08-06 01:49:18 -030079}