1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 29a163ed8SThomas Gleixner /* 3835c34a1SDave Jones * Memory preserving reboot related code. 49a163ed8SThomas Gleixner * 59a163ed8SThomas Gleixner * Created by: Hariprasad Nellitheertha (hari@in.ibm.com) 69a163ed8SThomas Gleixner * Copyright (C) IBM Corporation, 2004. All rights reserved 79a163ed8SThomas Gleixner */ 89a163ed8SThomas Gleixner 95a0e3ad6STejun Heo #include <linux/slab.h> 109a163ed8SThomas Gleixner #include <linux/errno.h> 119a163ed8SThomas Gleixner #include <linux/highmem.h> 129a163ed8SThomas Gleixner #include <linux/crash_dump.h> 139a163ed8SThomas Gleixner 147c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 159a163ed8SThomas Gleixner 1672ed7de7SJiri Slaby static inline bool is_crashed_pfn_valid(unsigned long pfn) 1772ed7de7SJiri Slaby { 1872ed7de7SJiri Slaby #ifndef CONFIG_X86_PAE 1972ed7de7SJiri Slaby /* 2072ed7de7SJiri Slaby * non-PAE kdump kernel executed from a PAE one will crop high pte 2172ed7de7SJiri Slaby * bits and poke unwanted space counting again from address 0, we 2272ed7de7SJiri Slaby * don't want that. pte must fit into unsigned long. In fact the 2372ed7de7SJiri Slaby * test checks high 12 bits for being zero (pfn will be shifted left 2472ed7de7SJiri Slaby * by PAGE_SHIFT). 2572ed7de7SJiri Slaby */ 2672ed7de7SJiri Slaby return pte_pfn(pfn_pte(pfn, __pgprot(0))) == pfn; 2772ed7de7SJiri Slaby #else 2872ed7de7SJiri Slaby return true; 2972ed7de7SJiri Slaby #endif 3072ed7de7SJiri Slaby } 3172ed7de7SJiri Slaby 329a163ed8SThomas Gleixner /** 339a163ed8SThomas Gleixner * copy_oldmem_page - copy one page from "oldmem" 349a163ed8SThomas Gleixner * @pfn: page frame number to be copied 359a163ed8SThomas Gleixner * @buf: target memory address for the copy; this can be in kernel address 369a163ed8SThomas Gleixner * space or user address space (see @userbuf) 379a163ed8SThomas Gleixner * @csize: number of bytes to copy 389a163ed8SThomas Gleixner * @offset: offset in bytes into the page (based on pfn) to begin the copy 399a163ed8SThomas Gleixner * @userbuf: if set, @buf is in user address space, use copy_to_user(), 409a163ed8SThomas Gleixner * otherwise @buf is in kernel address space, use memcpy(). 419a163ed8SThomas Gleixner * 42*7e015a27SThomas Gleixner * Copy a page from "oldmem". For this page, there might be no pte mapped 43*7e015a27SThomas Gleixner * in the current kernel. 449a163ed8SThomas Gleixner */ 45*7e015a27SThomas Gleixner ssize_t copy_oldmem_page(unsigned long pfn, char *buf, size_t csize, 46*7e015a27SThomas Gleixner unsigned long offset, int userbuf) 479a163ed8SThomas Gleixner { 489a163ed8SThomas Gleixner void *vaddr; 499a163ed8SThomas Gleixner 509a163ed8SThomas Gleixner if (!csize) 519a163ed8SThomas Gleixner return 0; 529a163ed8SThomas Gleixner 5372ed7de7SJiri Slaby if (!is_crashed_pfn_valid(pfn)) 5472ed7de7SJiri Slaby return -EFAULT; 5572ed7de7SJiri Slaby 56*7e015a27SThomas Gleixner vaddr = kmap_local_pfn(pfn); 579a163ed8SThomas Gleixner 589a163ed8SThomas Gleixner if (!userbuf) { 59*7e015a27SThomas Gleixner memcpy(buf, vaddr + offset, csize); 609a163ed8SThomas Gleixner } else { 61*7e015a27SThomas Gleixner if (copy_to_user(buf, vaddr + offset, csize)) 62*7e015a27SThomas Gleixner csize = -EFAULT; 639a163ed8SThomas Gleixner } 64*7e015a27SThomas Gleixner 65*7e015a27SThomas Gleixner kunmap_local(vaddr); 669a163ed8SThomas Gleixner 679a163ed8SThomas Gleixner return csize; 689a163ed8SThomas Gleixner } 69