Jesse Keating 7a32965
From df42d15cd28f468ecd4c30465b98a53cce90617c Mon Sep 17 00:00:00 2001
Jesse Keating 7a32965
From: Kyle McMartin <kyle@phobos.i.jkkm.org>
Jesse Keating 7a32965
Date: Tue, 30 Mar 2010 00:16:25 -0400
Jesse Keating 7a32965
Subject: dev-crash-driver.patch
Jesse Keating 7a32965
Jesse Keating 7a32965
---
Jesse Keating 7a32965
 arch/ia64/include/asm/crash.h |   90 +++++++++++++++++++++++++++++
Jesse Keating 7a32965
 arch/ia64/kernel/ia64_ksyms.c |    3 +
Jesse Keating 7a32965
 arch/x86/include/asm/crash.h  |   75 ++++++++++++++++++++++++
Jesse Keating 7a32965
 arch/x86/mm/ioremap.c         |    2 +
Jesse Keating 7a32965
 drivers/char/Kconfig          |    2 +
Jesse Keating 7a32965
 drivers/char/Makefile         |    2 +
Jesse Keating 7a32965
 drivers/char/crash.c          |  128 +++++++++++++++++++++++++++++++++++++++++
Jesse Keating 7a32965
 7 files changed, 302 insertions(+), 0 deletions(-)
Jesse Keating 7a32965
 create mode 100644 arch/ia64/include/asm/crash.h
Jesse Keating 7a32965
 create mode 100644 arch/x86/include/asm/crash.h
Jesse Keating 7a32965
 create mode 100644 drivers/char/crash.c
Jesse Keating 7a32965
Jesse Keating 7a32965
diff --git a/arch/ia64/include/asm/crash.h b/arch/ia64/include/asm/crash.h
Jesse Keating 7a32965
new file mode 100644
Jesse Keating 7a32965
index 0000000..541af84
Jesse Keating 7a32965
--- /dev/null
Jesse Keating 7a32965
+++ b/arch/ia64/include/asm/crash.h
Jesse Keating 7a32965
@@ -0,0 +1,90 @@
Jesse Keating 7a32965
+#ifndef _ASM_IA64_CRASH_H
Jesse Keating 7a32965
+#define _ASM_IA64_CRASH_H
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+/*
Jesse Keating 7a32965
+ * linux/include/asm-ia64/crash.h
Jesse Keating 7a32965
+ *
Jesse Keating 7a32965
+ * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
Jesse Keating 7a32965
+ *
Jesse Keating 7a32965
+ * This program is free software; you can redistribute it and/or modify
Jesse Keating 7a32965
+ * it under the terms of the GNU General Public License as published by
Jesse Keating 7a32965
+ * the Free Software Foundation; either version 2, or (at your option)
Jesse Keating 7a32965
+ * any later version.
Jesse Keating 7a32965
+ *
Jesse Keating 7a32965
+ * This program is distributed in the hope that it will be useful,
Jesse Keating 7a32965
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jesse Keating 7a32965
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Jesse Keating 7a32965
+ * GNU General Public License for more details.
Jesse Keating 7a32965
+ *
Jesse Keating 7a32965
+ * You should have received a copy of the GNU General Public License
Jesse Keating 7a32965
+ * along with this program; if not, write to the Free Software
Jesse Keating 7a32965
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Jesse Keating 7a32965
+ *
Jesse Keating 7a32965
+ */
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+#ifdef __KERNEL__
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+#include <linux/efi.h>
Jesse Keating 7a32965
+#include <linux/mm.h>
Jesse Keating 7a32965
+#include <asm/mmzone.h>
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+static inline void *
Jesse Keating 7a32965
+map_virtual(u64 offset, struct page **pp)
Jesse Keating 7a32965
+{
Jesse Keating 7a32965
+	struct page *page;
Jesse Keating 7a32965
+	unsigned long pfn;
Jesse Keating 7a32965
+	u32 type;
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	if (REGION_NUMBER(offset) == 5) {
Jesse Keating 7a32965
+		char byte;
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+		if (__get_user(byte, (char *)offset) == 0)
Jesse Keating 7a32965
+			return (void *)offset;
Jesse Keating 7a32965
+		else
Jesse Keating 7a32965
+			return NULL;
Jesse Keating 7a32965
+	}
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	switch (type = efi_mem_type(offset)) 
Jesse Keating 7a32965
+	{
Jesse Keating 7a32965
+	case EFI_LOADER_CODE:
Jesse Keating 7a32965
+	case EFI_LOADER_DATA:
Jesse Keating 7a32965
+	case EFI_BOOT_SERVICES_CODE:
Jesse Keating 7a32965
+	case EFI_BOOT_SERVICES_DATA:
Jesse Keating 7a32965
+	case EFI_CONVENTIONAL_MEMORY:
Jesse Keating 7a32965
+		break;
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	default:
Jesse Keating 7a32965
+		printk(KERN_INFO
Jesse Keating 7a32965
+		    "crash memory driver: invalid memory type for %lx: %d\n", 
Jesse Keating 7a32965
+			offset, type);
Jesse Keating 7a32965
+		return NULL;
Jesse Keating 7a32965
+	}
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	pfn = offset >> PAGE_SHIFT;
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	if (!pfn_valid(pfn)) {
Jesse Keating 7a32965
+		printk(KERN_INFO
Jesse Keating 7a32965
+			"crash memory driver: invalid pfn: %lx )\n", pfn);
Jesse Keating 7a32965
+		return NULL;
Jesse Keating 7a32965
+	}
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	page = pfn_to_page(pfn);
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	if (!page->virtual) {
Jesse Keating 7a32965
+		printk(KERN_INFO
Jesse Keating 7a32965
+		    "crash memory driver: offset: %lx page: %lx page->virtual: NULL\n", 
Jesse Keating 7a32965
+			offset, (unsigned long)page);
Jesse Keating 7a32965
+		return NULL;
Jesse Keating 7a32965
+	}
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	return (page->virtual + (offset & (PAGE_SIZE-1)));
Jesse Keating 7a32965
+}
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+static inline void unmap_virtual(struct page *page) 
Jesse Keating 7a32965
+{ 
Jesse Keating 7a32965
+	return;
Jesse Keating 7a32965
+}
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+#endif /* __KERNEL__ */
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+#endif /* _ASM_IA64_CRASH_H */
Jesse Keating 7a32965
diff --git a/arch/ia64/kernel/ia64_ksyms.c b/arch/ia64/kernel/ia64_ksyms.c
Jesse Keating 7a32965
index 7f4a0ed..552fe24 100644
Jesse Keating 7a32965
--- a/arch/ia64/kernel/ia64_ksyms.c
Jesse Keating 7a32965
+++ b/arch/ia64/kernel/ia64_ksyms.c
Jesse Keating 7a32965
@@ -84,6 +84,9 @@ EXPORT_SYMBOL(ia64_save_scratch_fpregs);
Jesse Keating 7a32965
 #include <asm/unwind.h>
Jesse Keating 7a32965
 EXPORT_SYMBOL(unw_init_running);
Jesse Keating 7a32965
 
Jesse Keating 7a32965
+#include <linux/efi.h>
Jesse Keating 7a32965
+EXPORT_SYMBOL_GPL(efi_mem_type);
Jesse Keating 7a32965
+
Jesse Keating 7a32965
 #if defined(CONFIG_IA64_ESI) || defined(CONFIG_IA64_ESI_MODULE)
Jesse Keating 7a32965
 extern void esi_call_phys (void);
Jesse Keating 7a32965
 EXPORT_SYMBOL_GPL(esi_call_phys);
Jesse Keating 7a32965
diff --git a/arch/x86/include/asm/crash.h b/arch/x86/include/asm/crash.h
Jesse Keating 7a32965
new file mode 100644
Jesse Keating 7a32965
index 0000000..dfcc006
Jesse Keating 7a32965
--- /dev/null
Jesse Keating 7a32965
+++ b/arch/x86/include/asm/crash.h
Jesse Keating 7a32965
@@ -0,0 +1,75 @@
Jesse Keating 7a32965
+#ifndef _ASM_I386_CRASH_H
Jesse Keating 7a32965
+#define _ASM_I386_CRASH_H
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+/*
Jesse Keating 7a32965
+ * linux/include/asm-i386/crash.h
Jesse Keating 7a32965
+ *
Jesse Keating 7a32965
+ * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
Jesse Keating 7a32965
+ *
Jesse Keating 7a32965
+ * This program is free software; you can redistribute it and/or modify
Jesse Keating 7a32965
+ * it under the terms of the GNU General Public License as published by
Jesse Keating 7a32965
+ * the Free Software Foundation; either version 2, or (at your option)
Jesse Keating 7a32965
+ * any later version.
Jesse Keating 7a32965
+ *
Jesse Keating 7a32965
+ * This program is distributed in the hope that it will be useful,
Jesse Keating 7a32965
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jesse Keating 7a32965
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Jesse Keating 7a32965
+ * GNU General Public License for more details.
Jesse Keating 7a32965
+ *
Jesse Keating 7a32965
+ * You should have received a copy of the GNU General Public License
Jesse Keating 7a32965
+ * along with this program; if not, write to the Free Software
Jesse Keating 7a32965
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Jesse Keating 7a32965
+ *
Jesse Keating 7a32965
+ */
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+#ifdef __KERNEL__
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+#include <linux/mm.h>
Jesse Keating 7a32965
+#include <linux/highmem.h>
Jesse Keating 7a32965
+#include <asm/mmzone.h>
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+extern int page_is_ram(unsigned long);
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+static inline void *
Jesse Keating 7a32965
+map_virtual(u64 offset, struct page **pp)
Jesse Keating 7a32965
+{
Jesse Keating 7a32965
+	struct page *page;
Jesse Keating 7a32965
+	unsigned long pfn;
Jesse Keating 7a32965
+	void *vaddr;
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	pfn = (unsigned long)(offset >> PAGE_SHIFT);
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	if (!page_is_ram(pfn)) {
Jesse Keating 7a32965
+		printk(KERN_INFO
Jesse Keating 7a32965
+		    "crash memory driver: !page_is_ram(pfn: %lx)\n", pfn);
Jesse Keating 7a32965
+		return NULL;
Jesse Keating 7a32965
+	}
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	if (!pfn_valid(pfn)) {
Jesse Keating 7a32965
+		printk(KERN_INFO
Jesse Keating 7a32965
+		    "crash memory driver: invalid pfn: %lx )\n", pfn);
Jesse Keating 7a32965
+		return NULL;
Jesse Keating 7a32965
+	}
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	page = pfn_to_page(pfn);
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	vaddr = kmap(page);
Jesse Keating 7a32965
+	if (!vaddr) {
Jesse Keating 7a32965
+		printk(KERN_INFO
Jesse Keating 7a32965
+		    "crash memory driver: pfn: %lx kmap(page: %lx) failed\n", 
Jesse Keating 7a32965
+			pfn, (unsigned long)page);
Jesse Keating 7a32965
+		return NULL;
Jesse Keating 7a32965
+	}
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	*pp = page;
Jesse Keating 7a32965
+	return (vaddr + (offset & (PAGE_SIZE-1)));
Jesse Keating 7a32965
+}
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+static inline void unmap_virtual(struct page *page) 
Jesse Keating 7a32965
+{ 
Jesse Keating 7a32965
+	kunmap(page);
Jesse Keating 7a32965
+}
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+#endif /* __KERNEL__ */
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+#endif /* _ASM_I386_CRASH_H */
Jesse Keating 7a32965
diff --git a/arch/x86/mm/ioremap.c b/arch/x86/mm/ioremap.c
Jesse Keating 7a32965
index 5eb1ba7..3e525d2 100644
Jesse Keating 7a32965
--- a/arch/x86/mm/ioremap.c
Jesse Keating 7a32965
+++ b/arch/x86/mm/ioremap.c
Jesse Keating 7a32965
@@ -24,6 +24,8 @@
Jesse Keating 7a32965
 
Jesse Keating 7a32965
 #include "physaddr.h"
Jesse Keating 7a32965
 
Jesse Keating 7a32965
+EXPORT_SYMBOL_GPL(page_is_ram);
Jesse Keating 7a32965
+
Jesse Keating 7a32965
 /*
Jesse Keating 7a32965
  * Fix up the linear direct mapping of the kernel to avoid cache attribute
Jesse Keating 7a32965
  * conflicts.
Jesse Keating 7a32965
diff --git a/drivers/char/Kconfig b/drivers/char/Kconfig
Jesse Keating 7a32965
index 3141dd3..153658c 100644
Jesse Keating 7a32965
--- a/drivers/char/Kconfig
Jesse Keating 7a32965
+++ b/drivers/char/Kconfig
Jesse Keating 7a32965
@@ -471,6 +471,8 @@ config LEGACY_PTYS
Jesse Keating 7a32965
 	  security.  This option enables these legacy devices; on most
Jesse Keating 7a32965
 	  systems, it is safe to say N.
Jesse Keating 7a32965
 
Jesse Keating 7a32965
+config CRASH
Jesse Keating 7a32965
+        tristate "Crash Utility memory driver"
Jesse Keating 7a32965
 
Jesse Keating 7a32965
 config LEGACY_PTY_COUNT
Jesse Keating 7a32965
 	int "Maximum number of legacy PTY in use"
Jesse Keating 7a32965
diff --git a/drivers/char/Makefile b/drivers/char/Makefile
Jesse Keating 7a32965
index f957edf..604c418 100644
Jesse Keating 7a32965
--- a/drivers/char/Makefile
Jesse Keating 7a32965
+++ b/drivers/char/Makefile
Jesse Keating 7a32965
@@ -111,6 +111,8 @@ obj-$(CONFIG_PS3_FLASH)		+= ps3flash.o
Jesse Keating 7a32965
 obj-$(CONFIG_JS_RTC)		+= js-rtc.o
Jesse Keating 7a32965
 js-rtc-y = rtc.o
Jesse Keating 7a32965
 
Jesse Keating 7a32965
+obj-$(CONFIG_CRASH)		+= crash.o
Jesse Keating 7a32965
+
Jesse Keating 7a32965
 # Files generated that shall be removed upon make clean
Jesse Keating 7a32965
 clean-files := consolemap_deftbl.c defkeymap.c
Jesse Keating 7a32965
 
Jesse Keating 7a32965
diff --git a/drivers/char/crash.c b/drivers/char/crash.c
Jesse Keating 7a32965
new file mode 100644
Jesse Keating 7a32965
index 0000000..e5437de
Jesse Keating 7a32965
--- /dev/null
Jesse Keating 7a32965
+++ b/drivers/char/crash.c
Jesse Keating 7a32965
@@ -0,0 +1,128 @@
Jesse Keating 7a32965
+/*
Jesse Keating 7a32965
+ *  linux/drivers/char/crash.c
Jesse Keating 7a32965
+ *
Jesse Keating 7a32965
+ *  Copyright (C) 2004  Dave Anderson <anderson@redhat.com>
Jesse Keating 7a32965
+ *  Copyright (C) 2004  Red Hat, Inc.
Jesse Keating 7a32965
+ */
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+/******************************************************************************
Jesse Keating 7a32965
+ *
Jesse Keating 7a32965
+ *   This program is free software; you can redistribute it and/or modify
Jesse Keating 7a32965
+ *   it under the terms of the GNU General Public License as published by
Jesse Keating 7a32965
+ *   the Free Software Foundation; either version 2, or (at your option)
Jesse Keating 7a32965
+ *   any later version.
Jesse Keating 7a32965
+ *
Jesse Keating 7a32965
+ *   This program is distributed in the hope that it will be useful,
Jesse Keating 7a32965
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
Jesse Keating 7a32965
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Jesse Keating 7a32965
+ *   GNU General Public License for more details.
Jesse Keating 7a32965
+ *
Jesse Keating 7a32965
+ *   You should have received a copy of the GNU General Public License
Jesse Keating 7a32965
+ *   along with this program; if not, write to the Free Software
Jesse Keating 7a32965
+ *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Jesse Keating 7a32965
+ *
Jesse Keating 7a32965
+ *****************************************************************************/
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+#include <linux/module.h>
Jesse Keating 7a32965
+#include <linux/types.h>
Jesse Keating 7a32965
+#include <linux/miscdevice.h>
Jesse Keating 7a32965
+#include <linux/init.h>
Jesse Keating 7a32965
+#include <asm/io.h>
Jesse Keating 7a32965
+#include <asm/uaccess.h>
Jesse Keating 7a32965
+#include <asm/types.h>
Jesse Keating 7a32965
+#include <asm/crash.h>
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+#define CRASH_VERSION   "1.0"
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+/*
Jesse Keating 7a32965
+ *  These are the file operation functions that allow crash utility
Jesse Keating 7a32965
+ *  access to physical memory.
Jesse Keating 7a32965
+ */
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+static loff_t 
Jesse Keating 7a32965
+crash_llseek(struct file * file, loff_t offset, int orig)
Jesse Keating 7a32965
+{
Jesse Keating 7a32965
+	switch (orig) {
Jesse Keating 7a32965
+	case 0:
Jesse Keating 7a32965
+		file->f_pos = offset;
Jesse Keating 7a32965
+		return file->f_pos;
Jesse Keating 7a32965
+	case 1:
Jesse Keating 7a32965
+		file->f_pos += offset;
Jesse Keating 7a32965
+		return file->f_pos;
Jesse Keating 7a32965
+	default:
Jesse Keating 7a32965
+		return -EINVAL;
Jesse Keating 7a32965
+	}
Jesse Keating 7a32965
+}
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+/*
Jesse Keating 7a32965
+ *  Determine the page address for an address offset value, 
Jesse Keating 7a32965
+ *  get a virtual address for it, and copy it out.
Jesse Keating 7a32965
+ *  Accesses must fit within a page.
Jesse Keating 7a32965
+ */
Jesse Keating 7a32965
+static ssize_t
Jesse Keating 7a32965
+crash_read(struct file *file, char *buf, size_t count, loff_t *poff)
Jesse Keating 7a32965
+{
Jesse Keating 7a32965
+	void *vaddr;
Jesse Keating 7a32965
+	struct page *page;
Jesse Keating 7a32965
+	u64 offset;
Jesse Keating 7a32965
+	ssize_t read;
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	offset = *poff;
Jesse Keating 7a32965
+	if (offset >> PAGE_SHIFT != (offset+count-1) >> PAGE_SHIFT) 
Jesse Keating 7a32965
+		return -EINVAL;
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	vaddr = map_virtual(offset, &page);
Jesse Keating 7a32965
+	if (!vaddr)
Jesse Keating 7a32965
+		return -EFAULT;
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	if (copy_to_user(buf, vaddr, count)) {
Jesse Keating 7a32965
+		unmap_virtual(page);
Jesse Keating 7a32965
+		return -EFAULT;
Jesse Keating 7a32965
+	}
Jesse Keating 7a32965
+	unmap_virtual(page);
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	read = count;
Jesse Keating 7a32965
+	*poff += read;
Jesse Keating 7a32965
+	return read;
Jesse Keating 7a32965
+}
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+static struct file_operations crash_fops = {
Jesse Keating 7a32965
+	.owner = THIS_MODULE,
Jesse Keating 7a32965
+	.llseek = crash_llseek,
Jesse Keating 7a32965
+	.read = crash_read,
Jesse Keating 7a32965
+};
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+static struct miscdevice crash_dev = {
Jesse Keating 7a32965
+	MISC_DYNAMIC_MINOR,
Jesse Keating 7a32965
+	"crash",
Jesse Keating 7a32965
+	&crash_fops
Jesse Keating 7a32965
+};
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+static int __init
Jesse Keating 7a32965
+crash_init(void)
Jesse Keating 7a32965
+{
Jesse Keating 7a32965
+	int ret;
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+	ret = misc_register(&crash_dev);
Jesse Keating 7a32965
+	if (ret) {
Jesse Keating 7a32965
+		printk(KERN_ERR 
Jesse Keating 7a32965
+		    "crash memory driver: cannot misc_register (MISC_DYNAMIC_MINOR)\n");
Jesse Keating 7a32965
+		goto out;
Jesse Keating 7a32965
+	}
Jesse Keating 7a32965
+	
Jesse Keating 7a32965
+	ret = 0;
Jesse Keating 7a32965
+	printk(KERN_INFO "crash memory driver: version %s\n", CRASH_VERSION);
Jesse Keating 7a32965
+out:
Jesse Keating 7a32965
+	return ret;
Jesse Keating 7a32965
+}
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+static void __exit
Jesse Keating 7a32965
+crash_cleanup_module(void)
Jesse Keating 7a32965
+{
Jesse Keating 7a32965
+	misc_deregister(&crash_dev);
Jesse Keating 7a32965
+}
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+module_init(crash_init);
Jesse Keating 7a32965
+module_exit(crash_cleanup_module);
Jesse Keating 7a32965
+
Jesse Keating 7a32965
+MODULE_LICENSE("GPL");
Jesse Keating 7a32965
-- 
Jesse Keating 7a32965
1.7.0.1
Jesse Keating 7a32965