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 @@ -1296,6 +1296,10 @@ config RELAY
28 + bool "Crash logging"
29 + depends on (!NO_BOOTMEM || HAVE_MEMBLOCK)
32 bool "Initial RAM filesystem and RAM disk (initramfs/initrd) support"
33 depends on BROKEN || !FRV
36 @@ -103,6 +103,7 @@ obj-$(CONFIG_TORTURE_TEST) += torture.o
37 obj-$(CONFIG_MEMBARRIER) += membarrier.o
39 obj-$(CONFIG_HAS_IOMEM) += memremap.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@nbd.name>
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>
78 +#include <linux/vmalloc.h>
81 +#define CRASHLOG_PAGES 4
82 +#define CRASHLOG_SIZE (CRASHLOG_PAGES * PAGE_SIZE)
83 +#define CRASHLOG_MAGIC 0xa1eedead
86 + * Start the log at 1M before the end of RAM, as some boot loaders like
87 + * to use the end of the RAM for stack usage and other things
88 + * If this fails, fall back to using the last part.
90 +#define CRASHLOG_OFFSET (1024 * 1024)
92 +struct crashlog_data {
98 +static struct debugfs_blob_wrapper crashlog_blob;
99 +static unsigned long crashlog_addr = 0;
100 +static struct crashlog_data *crashlog_buf;
101 +static struct kmsg_dumper dump;
102 +static bool first = true;
104 +extern struct list_head *crashlog_modules;
106 +static bool crashlog_set_addr(phys_addr_t addr, phys_addr_t size)
108 + /* Limit to lower 64 MB to avoid highmem */
109 + phys_addr_t limit = 64 * 1024 * 1024;
117 + if (addr + size > limit)
118 + size = limit - addr;
120 + crashlog_addr = addr;
122 + if (addr + size > CRASHLOG_OFFSET)
123 + crashlog_addr += size - CRASHLOG_OFFSET;
128 +#ifndef CONFIG_NO_BOOTMEM
129 +void __init crashlog_init_bootmem(bootmem_data_t *bdata)
131 + phys_addr_t start, end;
133 + start = PFN_PHYS(bdata->node_low_pfn);
134 + end = PFN_PHYS(bdata->node_min_pfn);
135 + if (!crashlog_set_addr(start, end - start))
138 + if (reserve_bootmem(crashlog_addr, CRASHLOG_SIZE, BOOTMEM_EXCLUSIVE) < 0) {
139 + printk("Crashlog failed to allocate RAM at address 0x%lx\n",
146 +#ifdef CONFIG_HAVE_MEMBLOCK
147 +void __init_memblock crashlog_init_memblock(phys_addr_t addr, phys_addr_t size)
149 + if (!crashlog_set_addr(addr, size))
152 + if (memblock_reserve(crashlog_addr, CRASHLOG_SIZE)) {
153 + printk("Crashlog failed to allocate RAM at address 0x%lx\n",
160 +static void __init crashlog_copy(void)
162 + if (crashlog_buf->magic != CRASHLOG_MAGIC)
165 + if (!crashlog_buf->len || crashlog_buf->len >
166 + CRASHLOG_SIZE - sizeof(*crashlog_buf))
169 + crashlog_blob.size = crashlog_buf->len;
170 + crashlog_blob.data = kmemdup(crashlog_buf->data,
171 + crashlog_buf->len, GFP_KERNEL);
173 + debugfs_create_blob("crashlog", 0700, NULL, &crashlog_blob);
176 +static int get_maxlen(void)
178 + return CRASHLOG_SIZE - sizeof(*crashlog_buf) - crashlog_buf->len;
181 +static void crashlog_printf(const char *fmt, ...)
184 + int len = get_maxlen();
189 + va_start(args, fmt);
190 + crashlog_buf->len += vscnprintf(
191 + &crashlog_buf->data[crashlog_buf->len],
196 +static void crashlog_do_dump(struct kmsg_dumper *dumper,
197 + enum kmsg_dump_reason reason)
205 + crashlog_printf("\n===================================\n");
207 + do_gettimeofday(&tv);
208 + crashlog_printf("Time: %lu.%lu\n",
209 + (long)tv.tv_sec, (long)tv.tv_usec);
212 + crashlog_printf("Modules:");
213 + list_for_each_entry(m, crashlog_modules, list) {
214 + crashlog_printf("\t%s@%p+%x", m->name,
215 + m->module_core, m->core_size,
216 + m->module_init, m->init_size);
218 + crashlog_printf("\n");
222 + buf = (char *)&crashlog_buf->data[crashlog_buf->len];
224 + kmsg_dump_get_buffer(dumper, true, buf, get_maxlen(), &len);
226 + crashlog_buf->len += len;
230 +int __init crashlog_init_fs(void)
232 + struct page *pages[CRASHLOG_PAGES];
236 + if (!crashlog_addr) {
237 + printk("No memory allocated for crashlog\n");
241 + printk("Crashlog allocated RAM at address 0x%lx\n", (unsigned long) crashlog_addr);
242 + for (i = 0; i < CRASHLOG_PAGES; i++)
243 + pages[i] = pfn_to_page((crashlog_addr >> PAGE_SHIFT) + i);
245 + prot = pgprot_writecombine(PAGE_KERNEL);
246 + crashlog_buf = vmap(pages, CRASHLOG_PAGES, VM_MAP, prot);
250 + crashlog_buf->magic = CRASHLOG_MAGIC;
251 + crashlog_buf->len = 0;
253 + dump.max_reason = KMSG_DUMP_OOPS;
254 + dump.dump = crashlog_do_dump;
255 + kmsg_dump_register(&dump);
259 +module_init(crashlog_init_fs);
260 --- a/kernel/module.c
261 +++ b/kernel/module.c
262 @@ -275,6 +275,9 @@ static void mod_update_bounds(struct mod
263 #ifdef CONFIG_KGDB_KDB
264 struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
265 #endif /* CONFIG_KGDB_KDB */
266 +#ifdef CONFIG_CRASHLOG
267 +struct list_head *crashlog_modules = &modules;
270 static void module_assert_mutex(void)
275 #include <linux/debugfs.h>
276 #include <linux/seq_file.h>
277 #include <linux/memblock.h>
278 +#include <linux/crashlog.h>
280 #include <asm-generic/sections.h>
281 #include <linux/io.h>
282 @@ -503,6 +504,8 @@ static void __init_memblock memblock_ins
283 memblock_set_region_node(rgn, nid);
285 type->total_size += size;
286 + if (type == &memblock.memory)
287 + crashlog_init_memblock(base, size);
291 @@ -541,6 +544,8 @@ int __init_memblock memblock_add_range(s
292 type->regions[0].flags = flags;
293 memblock_set_region_node(&type->regions[0], nid);
294 type->total_size = size;
295 + if (type == &memblock.memory)
296 + crashlog_init_memblock(base, size);
303 #include <linux/export.h>
304 #include <linux/kmemleak.h>
305 #include <linux/range.h>
306 +#include <linux/crashlog.h>
307 #include <linux/memblock.h>
308 #include <linux/bug.h>
309 #include <linux/io.h>
310 @@ -177,6 +178,7 @@ static unsigned long __init free_all_boo
311 if (!bdata->node_bootmem_map)
314 + crashlog_init_bootmem(bdata);
315 map = bdata->node_bootmem_map;
316 start = bdata->node_min_pfn;
317 end = bdata->node_low_pfn;