xref: /linux/arch/x86/kernel/crash_dump_32.c (revision 22124c9999f00340b062fff740db30187bf18454)
19a163ed8SThomas Gleixner /*
2835c34a1SDave Jones  *	Memory preserving reboot related code.
39a163ed8SThomas Gleixner  *
49a163ed8SThomas Gleixner  *	Created by: Hariprasad Nellitheertha (hari@in.ibm.com)
59a163ed8SThomas Gleixner  *	Copyright (C) IBM Corporation, 2004. All rights reserved
69a163ed8SThomas Gleixner  */
79a163ed8SThomas Gleixner 
89a163ed8SThomas Gleixner #include <linux/errno.h>
99a163ed8SThomas Gleixner #include <linux/highmem.h>
109a163ed8SThomas Gleixner #include <linux/crash_dump.h>
119a163ed8SThomas Gleixner 
129a163ed8SThomas Gleixner #include <asm/uaccess.h>
139a163ed8SThomas Gleixner 
149a163ed8SThomas Gleixner static void *kdump_buf_page;
159a163ed8SThomas Gleixner 
169a163ed8SThomas Gleixner /**
179a163ed8SThomas Gleixner  * copy_oldmem_page - copy one page from "oldmem"
189a163ed8SThomas Gleixner  * @pfn: page frame number to be copied
199a163ed8SThomas Gleixner  * @buf: target memory address for the copy; this can be in kernel address
209a163ed8SThomas Gleixner  *	space or user address space (see @userbuf)
219a163ed8SThomas Gleixner  * @csize: number of bytes to copy
229a163ed8SThomas Gleixner  * @offset: offset in bytes into the page (based on pfn) to begin the copy
239a163ed8SThomas Gleixner  * @userbuf: if set, @buf is in user address space, use copy_to_user(),
249a163ed8SThomas Gleixner  *	otherwise @buf is in kernel address space, use memcpy().
259a163ed8SThomas Gleixner  *
269a163ed8SThomas Gleixner  * Copy a page from "oldmem". For this page, there is no pte mapped
279a163ed8SThomas Gleixner  * in the current kernel. We stitch up a pte, similar to kmap_atomic.
289a163ed8SThomas Gleixner  *
299a163ed8SThomas Gleixner  * Calling copy_to_user() in atomic context is not desirable. Hence first
309a163ed8SThomas Gleixner  * copying the data to a pre-allocated kernel page and then copying to user
319a163ed8SThomas Gleixner  * space in non-atomic context.
329a163ed8SThomas Gleixner  */
339a163ed8SThomas Gleixner ssize_t copy_oldmem_page(unsigned long pfn, char *buf,
349a163ed8SThomas Gleixner                                size_t csize, unsigned long offset, int userbuf)
359a163ed8SThomas Gleixner {
369a163ed8SThomas Gleixner 	void  *vaddr;
379a163ed8SThomas Gleixner 
389a163ed8SThomas Gleixner 	if (!csize)
399a163ed8SThomas Gleixner 		return 0;
409a163ed8SThomas Gleixner 
419a163ed8SThomas Gleixner 	vaddr = kmap_atomic_pfn(pfn, KM_PTE0);
429a163ed8SThomas Gleixner 
439a163ed8SThomas Gleixner 	if (!userbuf) {
449a163ed8SThomas Gleixner 		memcpy(buf, (vaddr + offset), csize);
459a163ed8SThomas Gleixner 		kunmap_atomic(vaddr, KM_PTE0);
469a163ed8SThomas Gleixner 	} else {
479a163ed8SThomas Gleixner 		if (!kdump_buf_page) {
489a163ed8SThomas Gleixner 			printk(KERN_WARNING "Kdump: Kdump buffer page not"
499a163ed8SThomas Gleixner 				" allocated\n");
50*22124c99SFernando Luis Vázquez Cao 			kunmap_atomic(vaddr, KM_PTE0);
519a163ed8SThomas Gleixner 			return -EFAULT;
529a163ed8SThomas Gleixner 		}
539a163ed8SThomas Gleixner 		copy_page(kdump_buf_page, vaddr);
549a163ed8SThomas Gleixner 		kunmap_atomic(vaddr, KM_PTE0);
559a163ed8SThomas Gleixner 		if (copy_to_user(buf, (kdump_buf_page + offset), csize))
569a163ed8SThomas Gleixner 			return -EFAULT;
579a163ed8SThomas Gleixner 	}
589a163ed8SThomas Gleixner 
599a163ed8SThomas Gleixner 	return csize;
609a163ed8SThomas Gleixner }
619a163ed8SThomas Gleixner 
629a163ed8SThomas Gleixner static int __init kdump_buf_page_init(void)
639a163ed8SThomas Gleixner {
649a163ed8SThomas Gleixner 	int ret = 0;
659a163ed8SThomas Gleixner 
669a163ed8SThomas Gleixner 	kdump_buf_page = kmalloc(PAGE_SIZE, GFP_KERNEL);
679a163ed8SThomas Gleixner 	if (!kdump_buf_page) {
689a163ed8SThomas Gleixner 		printk(KERN_WARNING "Kdump: Failed to allocate kdump buffer"
699a163ed8SThomas Gleixner 			 " page\n");
709a163ed8SThomas Gleixner 		ret = -ENOMEM;
719a163ed8SThomas Gleixner 	}
729a163ed8SThomas Gleixner 
739a163ed8SThomas Gleixner 	return ret;
749a163ed8SThomas Gleixner }
759a163ed8SThomas Gleixner arch_initcall(kdump_buf_page_init);
76