/*1* Memory preserving reboot related code.2*3* Created by: Hariprasad Nellitheertha ([email protected])4* Copyright (C) IBM Corporation, 2004. All rights reserved5*/67#include <linux/slab.h>8#include <linux/errno.h>9#include <linux/highmem.h>10#include <linux/crash_dump.h>1112#include <asm/uaccess.h>1314static void *kdump_buf_page;1516static inline bool is_crashed_pfn_valid(unsigned long pfn)17{18#ifndef CONFIG_X86_PAE19/*20* non-PAE kdump kernel executed from a PAE one will crop high pte21* bits and poke unwanted space counting again from address 0, we22* don't want that. pte must fit into unsigned long. In fact the23* test checks high 12 bits for being zero (pfn will be shifted left24* by PAGE_SHIFT).25*/26return pte_pfn(pfn_pte(pfn, __pgprot(0))) == pfn;27#else28return true;29#endif30}3132/**33* copy_oldmem_page - copy one page from "oldmem"34* @pfn: page frame number to be copied35* @buf: target memory address for the copy; this can be in kernel address36* space or user address space (see @userbuf)37* @csize: number of bytes to copy38* @offset: offset in bytes into the page (based on pfn) to begin the copy39* @userbuf: if set, @buf is in user address space, use copy_to_user(),40* otherwise @buf is in kernel address space, use memcpy().41*42* Copy a page from "oldmem". For this page, there is no pte mapped43* in the current kernel. We stitch up a pte, similar to kmap_atomic.44*45* Calling copy_to_user() in atomic context is not desirable. Hence first46* copying the data to a pre-allocated kernel page and then copying to user47* space in non-atomic context.48*/49ssize_t copy_oldmem_page(unsigned long pfn, char *buf,50size_t csize, unsigned long offset, int userbuf)51{52void *vaddr;5354if (!csize)55return 0;5657if (!is_crashed_pfn_valid(pfn))58return -EFAULT;5960vaddr = kmap_atomic_pfn(pfn);6162if (!userbuf) {63memcpy(buf, (vaddr + offset), csize);64kunmap_atomic(vaddr, KM_PTE0);65} else {66if (!kdump_buf_page) {67printk(KERN_WARNING "Kdump: Kdump buffer page not"68" allocated\n");69kunmap_atomic(vaddr, KM_PTE0);70return -EFAULT;71}72copy_page(kdump_buf_page, vaddr);73kunmap_atomic(vaddr, KM_PTE0);74if (copy_to_user(buf, (kdump_buf_page + offset), csize))75return -EFAULT;76}7778return csize;79}8081static int __init kdump_buf_page_init(void)82{83int ret = 0;8485kdump_buf_page = kmalloc(PAGE_SIZE, GFP_KERNEL);86if (!kdump_buf_page) {87printk(KERN_WARNING "Kdump: Failed to allocate kdump buffer"88" page\n");89ret = -ENOMEM;90}9192return ret;93}94arch_initcall(kdump_buf_page_init);959697