2 +++ b/include/linux/crashlog.h
7 +#ifdef CONFIG_CRASHLOG
8 +void crashlog_init_bootmem(struct bootmem_data *bdata);
9 +void crashlog_init_memblock(phys_addr_t addr, phys_addr_t size);
11 +static inline void crashlog_init_bootmem(struct bootmem_data *bdata)
15 +static inline void crashlog_init_memblock(phys_addr_t addr, phys_addr_t size)
23 @@ -926,6 +926,10 @@ config RELAY
28 + bool "Crash logging"
29 + depends on (!NO_BOOTMEM || HAVE_MEMBLOCK) && !ARM
32 bool "Initial RAM filesystem and RAM disk (initramfs/initrd) support"
33 depends on BROKEN || !FRV
36 @@ -107,6 +107,7 @@ obj-$(CONFIG_USER_RETURN_NOTIFIER) += us
37 obj-$(CONFIG_PADATA) += padata.o
38 obj-$(CONFIG_CRASH_DUMP) += crash_dump.o
39 obj-$(CONFIG_JUMP_LABEL) += jump_label.o
40 +obj-$(CONFIG_CRASHLOG) += crashlog.o
42 $(obj)/configs.o: $(obj)/config_data.h
45 +++ b/kernel/crashlog.c
48 + * Crash information logger
49 + * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
51 + * Based on ramoops.c
52 + * Copyright (C) 2010 Marco Stornelli <marco.stornelli@gmail.com>
54 + * This program is free software; you can redistribute it and/or
55 + * modify it under the terms of the GNU General Public License
56 + * version 2 as published by the Free Software Foundation.
58 + * This program is distributed in the hope that it will be useful, but
59 + * WITHOUT ANY WARRANTY; without even the implied warranty of
60 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
61 + * General Public License for more details.
63 + * You should have received a copy of the GNU General Public License
64 + * along with this program; if not, write to the Free Software
65 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
70 +#include <linux/module.h>
71 +#include <linux/bootmem.h>
72 +#include <linux/memblock.h>
73 +#include <linux/debugfs.h>
74 +#include <linux/crashlog.h>
75 +#include <linux/kmsg_dump.h>
76 +#include <linux/module.h>
77 +#include <linux/pfn.h>
80 +#define CRASHLOG_PAGES 4
81 +#define CRASHLOG_SIZE (CRASHLOG_PAGES * PAGE_SIZE)
82 +#define CRASHLOG_MAGIC 0xa1eedead
85 + * Start the log at 1M before the end of RAM, as some boot loaders like
86 + * to use the end of the RAM for stack usage and other things
87 + * If this fails, fall back to using the last part.
89 +#define CRASHLOG_OFFSET (1024 * 1024)
91 +struct crashlog_data {
97 +static struct debugfs_blob_wrapper crashlog_blob;
98 +static unsigned long crashlog_addr = 0;
99 +static struct crashlog_data *crashlog_buf;
100 +static struct kmsg_dumper dump;
101 +static bool first = true;
103 +extern struct list_head *crashlog_modules;
105 +#ifndef CONFIG_NO_BOOTMEM
106 +void __init crashlog_init_bootmem(bootmem_data_t *bdata)
108 + unsigned long addr;
113 + addr = PFN_PHYS(bdata->node_low_pfn) - CRASHLOG_OFFSET;
114 + if (reserve_bootmem(addr, CRASHLOG_SIZE, BOOTMEM_EXCLUSIVE) < 0) {
115 + printk("Crashlog failed to allocate RAM at address 0x%lx\n", addr);
116 + bdata->node_low_pfn -= CRASHLOG_PAGES;
117 + addr = PFN_PHYS(bdata->node_low_pfn);
119 + crashlog_addr = addr;
123 +#ifdef CONFIG_HAVE_MEMBLOCK
124 +void __meminit crashlog_init_memblock(phys_addr_t addr, phys_addr_t size)
129 + addr += size - CRASHLOG_OFFSET;
130 + if (memblock_reserve(addr, CRASHLOG_SIZE)) {
131 + printk("Crashlog failed to allocate RAM at address 0x%lx\n", (unsigned long) addr);
135 + crashlog_addr = addr;
139 +static void __init crashlog_copy(void)
141 + if (crashlog_buf->magic != CRASHLOG_MAGIC)
144 + if (!crashlog_buf->len || crashlog_buf->len >
145 + CRASHLOG_SIZE - sizeof(*crashlog_buf))
148 + crashlog_blob.size = crashlog_buf->len;
149 + crashlog_blob.data = kmemdup(crashlog_buf->data,
150 + crashlog_buf->len, GFP_KERNEL);
152 + debugfs_create_blob("crashlog", 0700, NULL, &crashlog_blob);
155 +static int get_maxlen(void)
157 + return CRASHLOG_SIZE - sizeof(*crashlog_buf) - crashlog_buf->len;
160 +static void crashlog_printf(const char *fmt, ...)
163 + int len = get_maxlen();
168 + va_start(args, fmt);
169 + crashlog_buf->len += vsnprintf(
170 + &crashlog_buf->data[crashlog_buf->len],
175 +static void crashlog_do_dump(struct kmsg_dumper *dumper,
176 + enum kmsg_dump_reason reason, const char *s1, unsigned long l1,
177 + const char *s2, unsigned long l2)
179 + unsigned long s1_start, s2_start;
180 + unsigned long l1_cpy, l2_cpy;
187 + crashlog_printf("\n===================================\n");
189 + do_gettimeofday(&tv);
190 + crashlog_printf("Time: %lu.%lu\n",
191 + (long)tv.tv_sec, (long)tv.tv_usec);
194 + crashlog_printf("Modules:");
195 + list_for_each_entry(m, crashlog_modules, list) {
196 + crashlog_printf("\t%s@%p+%x", m->name,
197 + m->module_core, m->core_size,
198 + m->module_init, m->init_size);
200 + crashlog_printf("\n");
204 + buf = (char *)&crashlog_buf->data[crashlog_buf->len];
205 + len = get_maxlen();
207 + l2_cpy = min(l2, (unsigned long)len);
208 + l1_cpy = min(l1, (unsigned long)len - l2_cpy);
210 + s2_start = l2 - l2_cpy;
211 + s1_start = l1 - l1_cpy;
213 + memcpy(buf, s1 + s1_start, l1_cpy);
214 + memcpy(buf + l1_cpy, s2 + s2_start, l2_cpy);
215 + crashlog_buf->len += l1_cpy + l2_cpy;
219 +int __init crashlog_init_fs(void)
221 + if (!crashlog_addr)
224 + crashlog_buf = ioremap(crashlog_addr, CRASHLOG_SIZE);
228 + crashlog_buf->magic = CRASHLOG_MAGIC;
229 + crashlog_buf->len = 0;
231 + dump.dump = crashlog_do_dump;
232 + kmsg_dump_register(&dump);
236 +module_init(crashlog_init_fs);
240 #include <linux/export.h>
241 #include <linux/kmemleak.h>
242 #include <linux/range.h>
243 +#include <linux/crashlog.h>
244 #include <linux/memblock.h>
247 @@ -177,6 +178,7 @@ static unsigned long __init free_all_boo
248 if (!bdata->node_bootmem_map)
251 + crashlog_init_bootmem(bdata);
252 start = bdata->node_min_pfn;
253 end = bdata->node_low_pfn;
255 --- a/kernel/module.c
256 +++ b/kernel/module.c
257 @@ -101,6 +101,9 @@ static LIST_HEAD(modules);
258 #ifdef CONFIG_KGDB_KDB
259 struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
260 #endif /* CONFIG_KGDB_KDB */
261 +#ifdef CONFIG_CRASHLOG
262 +struct list_head *crashlog_modules = &modules;
266 /* Block module loading/unloading? */
270 #include <linux/debugfs.h>
271 #include <linux/seq_file.h>
272 #include <linux/memblock.h>
273 +#include <linux/crashlog.h>
275 static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
276 static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
277 @@ -305,6 +306,8 @@ static void __init_memblock memblock_ins
278 memblock_set_region_node(rgn, nid);
280 type->total_size += size;
281 + if (type == &memblock.memory && idx == 0)
282 + crashlog_init_memblock(base, size);