b1c38a70f26028b44e0cc1e95d90bc9edf6f449c
[openwrt/staging/chunkeey.git] / target / linux / generic / patches-3.18 / 930-crashlog.patch
1 --- /dev/null
2 +++ b/include/linux/crashlog.h
3 @@ -0,0 +1,17 @@
4 +#ifndef __CRASHLOG_H
5 +#define __CRASHLOG_H
6 +
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);
10 +#else
11 +static inline void crashlog_init_bootmem(struct bootmem_data *bdata)
12 +{
13 +}
14 +
15 +static inline void crashlog_init_memblock(phys_addr_t addr, phys_addr_t size)
16 +{
17 +}
18 +#endif
19 +
20 +#endif
21 --- a/init/Kconfig
22 +++ b/init/Kconfig
23 @@ -1279,6 +1279,10 @@ config RELAY
24
25 If unsure, say N.
26
27 +config CRASHLOG
28 + bool "Crash logging"
29 + depends on (!NO_BOOTMEM || HAVE_MEMBLOCK) && !(ARM || SPARC || PPC)
30 +
31 config BLK_DEV_INITRD
32 bool "Initial RAM filesystem and RAM disk (initramfs/initrd) support"
33 depends on BROKEN || !FRV
34 --- a/kernel/Makefile
35 +++ b/kernel/Makefile
36 @@ -96,6 +96,7 @@ obj-$(CONFIG_CRASH_DUMP) += crash_dump.o
37 obj-$(CONFIG_JUMP_LABEL) += jump_label.o
38 obj-$(CONFIG_CONTEXT_TRACKING) += context_tracking.o
39 obj-$(CONFIG_TORTURE_TEST) += torture.o
40 +obj-$(CONFIG_CRASHLOG) += crashlog.o
41
42 $(obj)/configs.o: $(obj)/config_data.h
43
44 --- /dev/null
45 +++ b/kernel/crashlog.c
46 @@ -0,0 +1,181 @@
47 +/*
48 + * Crash information logger
49 + * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
50 + *
51 + * Based on ramoops.c
52 + * Copyright (C) 2010 Marco Stornelli <marco.stornelli@gmail.com>
53 + *
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.
57 + *
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.
62 + *
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
66 + * 02110-1301 USA
67 + *
68 + */
69 +
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 <asm/io.h>
79 +
80 +#define CRASHLOG_PAGES 4
81 +#define CRASHLOG_SIZE (CRASHLOG_PAGES * PAGE_SIZE)
82 +#define CRASHLOG_MAGIC 0xa1eedead
83 +
84 +/*
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.
88 + */
89 +#define CRASHLOG_OFFSET (1024 * 1024)
90 +
91 +struct crashlog_data {
92 + u32 magic;
93 + u32 len;
94 + u8 data[];
95 +};
96 +
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;
102 +
103 +extern struct list_head *crashlog_modules;
104 +
105 +#ifndef CONFIG_NO_BOOTMEM
106 +void __init crashlog_init_bootmem(bootmem_data_t *bdata)
107 +{
108 + unsigned long addr;
109 +
110 + if (crashlog_addr)
111 + return;
112 +
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);
118 + }
119 + crashlog_addr = addr;
120 +}
121 +#endif
122 +
123 +#ifdef CONFIG_HAVE_MEMBLOCK
124 +void __meminit crashlog_init_memblock(phys_addr_t addr, phys_addr_t size)
125 +{
126 + if (crashlog_addr)
127 + return;
128 +
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);
132 + return;
133 + }
134 +
135 + crashlog_addr = addr;
136 +}
137 +#endif
138 +
139 +static void __init crashlog_copy(void)
140 +{
141 + if (crashlog_buf->magic != CRASHLOG_MAGIC)
142 + return;
143 +
144 + if (!crashlog_buf->len || crashlog_buf->len >
145 + CRASHLOG_SIZE - sizeof(*crashlog_buf))
146 + return;
147 +
148 + crashlog_blob.size = crashlog_buf->len;
149 + crashlog_blob.data = kmemdup(crashlog_buf->data,
150 + crashlog_buf->len, GFP_KERNEL);
151 +
152 + debugfs_create_blob("crashlog", 0700, NULL, &crashlog_blob);
153 +}
154 +
155 +static int get_maxlen(void)
156 +{
157 + return CRASHLOG_SIZE - sizeof(*crashlog_buf) - crashlog_buf->len;
158 +}
159 +
160 +static void crashlog_printf(const char *fmt, ...)
161 +{
162 + va_list args;
163 + int len = get_maxlen();
164 +
165 + if (!len)
166 + return;
167 +
168 + va_start(args, fmt);
169 + crashlog_buf->len += vscnprintf(
170 + &crashlog_buf->data[crashlog_buf->len],
171 + len, fmt, args);
172 + va_end(args);
173 +}
174 +
175 +static void crashlog_do_dump(struct kmsg_dumper *dumper,
176 + enum kmsg_dump_reason reason)
177 +{
178 + struct timeval tv;
179 + struct module *m;
180 + char *buf;
181 + size_t len;
182 +
183 + if (!first)
184 + crashlog_printf("\n===================================\n");
185 +
186 + do_gettimeofday(&tv);
187 + crashlog_printf("Time: %lu.%lu\n",
188 + (long)tv.tv_sec, (long)tv.tv_usec);
189 +
190 + if (first) {
191 + crashlog_printf("Modules:");
192 + list_for_each_entry(m, crashlog_modules, list) {
193 + crashlog_printf("\t%s@%p+%x", m->name,
194 + m->module_core, m->core_size,
195 + m->module_init, m->init_size);
196 + }
197 + crashlog_printf("\n");
198 + first = false;
199 + }
200 +
201 + buf = (char *)&crashlog_buf->data[crashlog_buf->len];
202 +
203 + kmsg_dump_get_buffer(dumper, true, buf, get_maxlen(), &len);
204 +
205 + crashlog_buf->len += len;
206 +}
207 +
208 +
209 +int __init crashlog_init_fs(void)
210 +{
211 + if (!crashlog_addr)
212 + return -ENOMEM;
213 +
214 + crashlog_buf = ioremap(crashlog_addr, CRASHLOG_SIZE);
215 +
216 + crashlog_copy();
217 +
218 + crashlog_buf->magic = CRASHLOG_MAGIC;
219 + crashlog_buf->len = 0;
220 +
221 + dump.max_reason = KMSG_DUMP_OOPS;
222 + dump.dump = crashlog_do_dump;
223 + kmsg_dump_register(&dump);
224 +
225 + return 0;
226 +}
227 +module_init(crashlog_init_fs);
228 --- a/mm/bootmem.c
229 +++ b/mm/bootmem.c
230 @@ -15,6 +15,7 @@
231 #include <linux/export.h>
232 #include <linux/kmemleak.h>
233 #include <linux/range.h>
234 +#include <linux/crashlog.h>
235 #include <linux/memblock.h>
236 #include <linux/bug.h>
237 #include <linux/io.h>
238 @@ -177,6 +178,7 @@ static unsigned long __init free_all_boo
239 if (!bdata->node_bootmem_map)
240 return 0;
241
242 + crashlog_init_bootmem(bdata);
243 map = bdata->node_bootmem_map;
244 start = bdata->node_min_pfn;
245 end = bdata->node_low_pfn;
246 --- a/kernel/module.c
247 +++ b/kernel/module.c
248 @@ -105,6 +105,9 @@ static LIST_HEAD(modules);
249 #ifdef CONFIG_KGDB_KDB
250 struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
251 #endif /* CONFIG_KGDB_KDB */
252 +#ifdef CONFIG_CRASHLOG
253 +struct list_head *crashlog_modules = &modules;
254 +#endif
255
256 #ifdef CONFIG_MODULE_SIG
257 #ifdef CONFIG_MODULE_SIG_FORCE
258 --- a/mm/memblock.c
259 +++ b/mm/memblock.c
260 @@ -19,6 +19,7 @@
261 #include <linux/debugfs.h>
262 #include <linux/seq_file.h>
263 #include <linux/memblock.h>
264 +#include <linux/crashlog.h>
265
266 #include <asm-generic/sections.h>
267 #include <linux/io.h>
268 @@ -477,6 +478,8 @@ static void __init_memblock memblock_ins
269 memblock_set_region_node(rgn, nid);
270 type->cnt++;
271 type->total_size += size;
272 + if (type == &memblock.memory && idx == 0)
273 + crashlog_init_memblock(base, size);
274 }
275
276 /**