xref: /linux/arch/x86/kernel/crash_dump_32.c (revision 57cac4d1880527e0baf6c2fda529d2ad1d815aec)
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 
16*57cac4d1SVivek Goyal /* Stores the physical address of elf header of crash image. */
17*57cac4d1SVivek Goyal unsigned long long elfcorehdr_addr = ELFCORE_ADDR_MAX;
18*57cac4d1SVivek Goyal 
199a163ed8SThomas Gleixner /**
209a163ed8SThomas Gleixner  * copy_oldmem_page - copy one page from "oldmem"
219a163ed8SThomas Gleixner  * @pfn: page frame number to be copied
229a163ed8SThomas Gleixner  * @buf: target memory address for the copy; this can be in kernel address
239a163ed8SThomas Gleixner  *	space or user address space (see @userbuf)
249a163ed8SThomas Gleixner  * @csize: number of bytes to copy
259a163ed8SThomas Gleixner  * @offset: offset in bytes into the page (based on pfn) to begin the copy
269a163ed8SThomas Gleixner  * @userbuf: if set, @buf is in user address space, use copy_to_user(),
279a163ed8SThomas Gleixner  *	otherwise @buf is in kernel address space, use memcpy().
289a163ed8SThomas Gleixner  *
299a163ed8SThomas Gleixner  * Copy a page from "oldmem". For this page, there is no pte mapped
309a163ed8SThomas Gleixner  * in the current kernel. We stitch up a pte, similar to kmap_atomic.
319a163ed8SThomas Gleixner  *
329a163ed8SThomas Gleixner  * Calling copy_to_user() in atomic context is not desirable. Hence first
339a163ed8SThomas Gleixner  * copying the data to a pre-allocated kernel page and then copying to user
349a163ed8SThomas Gleixner  * space in non-atomic context.
359a163ed8SThomas Gleixner  */
369a163ed8SThomas Gleixner ssize_t copy_oldmem_page(unsigned long pfn, char *buf,
379a163ed8SThomas Gleixner                                size_t csize, unsigned long offset, int userbuf)
389a163ed8SThomas Gleixner {
399a163ed8SThomas Gleixner 	void  *vaddr;
409a163ed8SThomas Gleixner 
419a163ed8SThomas Gleixner 	if (!csize)
429a163ed8SThomas Gleixner 		return 0;
439a163ed8SThomas Gleixner 
449a163ed8SThomas Gleixner 	vaddr = kmap_atomic_pfn(pfn, KM_PTE0);
459a163ed8SThomas Gleixner 
469a163ed8SThomas Gleixner 	if (!userbuf) {
479a163ed8SThomas Gleixner 		memcpy(buf, (vaddr + offset), csize);
489a163ed8SThomas Gleixner 		kunmap_atomic(vaddr, KM_PTE0);
499a163ed8SThomas Gleixner 	} else {
509a163ed8SThomas Gleixner 		if (!kdump_buf_page) {
519a163ed8SThomas Gleixner 			printk(KERN_WARNING "Kdump: Kdump buffer page not"
529a163ed8SThomas Gleixner 				" allocated\n");
5322124c99SFernando Luis Vázquez Cao 			kunmap_atomic(vaddr, KM_PTE0);
549a163ed8SThomas Gleixner 			return -EFAULT;
559a163ed8SThomas Gleixner 		}
569a163ed8SThomas Gleixner 		copy_page(kdump_buf_page, vaddr);
579a163ed8SThomas Gleixner 		kunmap_atomic(vaddr, KM_PTE0);
589a163ed8SThomas Gleixner 		if (copy_to_user(buf, (kdump_buf_page + offset), csize))
599a163ed8SThomas Gleixner 			return -EFAULT;
609a163ed8SThomas Gleixner 	}
619a163ed8SThomas Gleixner 
629a163ed8SThomas Gleixner 	return csize;
639a163ed8SThomas Gleixner }
649a163ed8SThomas Gleixner 
659a163ed8SThomas Gleixner static int __init kdump_buf_page_init(void)
669a163ed8SThomas Gleixner {
679a163ed8SThomas Gleixner 	int ret = 0;
689a163ed8SThomas Gleixner 
699a163ed8SThomas Gleixner 	kdump_buf_page = kmalloc(PAGE_SIZE, GFP_KERNEL);
709a163ed8SThomas Gleixner 	if (!kdump_buf_page) {
719a163ed8SThomas Gleixner 		printk(KERN_WARNING "Kdump: Failed to allocate kdump buffer"
729a163ed8SThomas Gleixner 			 " page\n");
739a163ed8SThomas Gleixner 		ret = -ENOMEM;
749a163ed8SThomas Gleixner 	}
759a163ed8SThomas Gleixner 
769a163ed8SThomas Gleixner 	return ret;
779a163ed8SThomas Gleixner }
789a163ed8SThomas Gleixner arch_initcall(kdump_buf_page_init);
79