4a6a89bc11a61a51a8cfcf8c6056388308276a92
[openwrt.git] / target / linux / generic / patches-2.6.35 / 930-crashlog.patch
1 --- /dev/null
2 +++ b/include/linux/crashlog.h
3 @@ -0,0 +1,12 @@
4 +#ifndef __CRASHLOG_H
5 +#define __CRASHLOG_H
6 +
7 +#ifdef CONFIG_CRASHLOG
8 +void __init crashlog_init_mem(struct bootmem_data *bdata);
9 +#else
10 +static inline void crashlog_init_mem(struct bootmem_data *bdata)
11 +{
12 +}
13 +#endif
14 +
15 +#endif
16 --- a/init/Kconfig
17 +++ b/init/Kconfig
18 @@ -749,6 +749,9 @@ config NET_NS
19           Allow user space to create what appear to be multiple instances
20           of the network stack.
21  
22 +config CRASHLOG
23 +       bool "Crash logging"
24 +
25  config BLK_DEV_INITRD
26         bool "Initial RAM filesystem and RAM disk (initramfs/initrd) support"
27         depends on BROKEN || !FRV
28 --- a/kernel/Makefile
29 +++ b/kernel/Makefile
30 @@ -105,6 +105,7 @@ obj-$(CONFIG_PERF_EVENTS) += perf_event.
31  obj-$(CONFIG_HAVE_HW_BREAKPOINT) += hw_breakpoint.o
32  obj-$(CONFIG_USER_RETURN_NOTIFIER) += user-return-notifier.o
33  obj-$(CONFIG_PADATA) += padata.o
34 +obj-$(CONFIG_CRASHLOG) += crashlog.o
35  
36  ifneq ($(CONFIG_SCHED_OMIT_FRAME_POINTER),y)
37  # According to Alan Modra <alan@linuxcare.com.au>, the -fno-omit-frame-pointer is
38 --- /dev/null
39 +++ b/kernel/crashlog.c
40 @@ -0,0 +1,171 @@
41 +/*
42 + * Crash information logger
43 + * Copyright (C) 2010 Felix Fietkau <nbd@openwrt.org>
44 + *
45 + * Based on ramoops.c
46 + *   Copyright (C) 2010 Marco Stornelli <marco.stornelli@gmail.com>
47 + *
48 + * This program is free software; you can redistribute it and/or
49 + * modify it under the terms of the GNU General Public License
50 + * version 2 as published by the Free Software Foundation.
51 + *
52 + * This program is distributed in the hope that it will be useful, but
53 + * WITHOUT ANY WARRANTY; without even the implied warranty of
54 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
55 + * General Public License for more details.
56 + *
57 + * You should have received a copy of the GNU General Public License
58 + * along with this program; if not, write to the Free Software
59 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
60 + * 02110-1301 USA
61 + *
62 + */
63 +
64 +#include <linux/module.h>
65 +#include <linux/bootmem.h>
66 +#include <linux/debugfs.h>
67 +#include <linux/crashlog.h>
68 +#include <linux/kmsg_dump.h>
69 +#include <linux/module.h>
70 +#include <linux/pfn.h>
71 +#include <asm/io.h>
72 +
73 +#define CRASHLOG_PAGES 4
74 +#define CRASHLOG_SIZE  (CRASHLOG_PAGES * PAGE_SIZE)
75 +#define CRASHLOG_MAGIC 0xa1eedead
76 +
77 +/*
78 + * Start the log at 1M before the end of RAM, as some boot loaders like
79 + * to use the end of the RAM for stack usage and other things
80 + * If this fails, fall back to using the last part.
81 + */
82 +#define CRASHLOG_OFFSET        (1024 * 1024)
83 +
84 +struct crashlog_data {
85 +       u32 magic;
86 +       u32 len;
87 +       u8 data[];
88 +};
89 +
90 +static struct debugfs_blob_wrapper crashlog_blob;
91 +static unsigned long crashlog_addr = 0;
92 +static struct crashlog_data *crashlog_buf;
93 +static struct kmsg_dumper dump;
94 +static bool first = true;
95 +
96 +extern struct list_head *crashlog_modules;
97 +
98 +void __init crashlog_init_mem(bootmem_data_t *bdata)
99 +{
100 +       unsigned long addr;
101 +
102 +       if (crashlog_addr)
103 +               return;
104 +
105 +       addr = PFN_PHYS(bdata->node_low_pfn) - CRASHLOG_OFFSET;
106 +       if (reserve_bootmem(addr, CRASHLOG_SIZE, BOOTMEM_EXCLUSIVE) < 0) {
107 +               printk("Crashlog failed to allocate RAM at address 0x%lx\n", addr);
108 +               bdata->node_low_pfn -= CRASHLOG_PAGES;
109 +               addr = PFN_PHYS(bdata->node_low_pfn);
110 +       }
111 +       crashlog_addr = addr;
112 +}
113 +
114 +static void __init crashlog_copy(void)
115 +{
116 +       if (crashlog_buf->magic != CRASHLOG_MAGIC)
117 +               return;
118 +
119 +       if (!crashlog_buf->len || crashlog_buf->len >
120 +           CRASHLOG_SIZE - sizeof(*crashlog_buf))
121 +               return;
122 +
123 +       crashlog_blob.size = crashlog_buf->len;
124 +       crashlog_blob.data = kmemdup(crashlog_buf->data,
125 +               crashlog_buf->len, GFP_KERNEL);
126 +
127 +       debugfs_create_blob("crashlog", 0700, NULL, &crashlog_blob);
128 +}
129 +
130 +static int get_maxlen(void)
131 +{
132 +       return CRASHLOG_SIZE - sizeof(*crashlog_buf) - crashlog_buf->len;
133 +}
134 +
135 +static void crashlog_printf(const char *fmt, ...)
136 +{
137 +       va_list args;
138 +       int len = get_maxlen();
139 +
140 +       if (!len)
141 +               return;
142 +
143 +       va_start(args, fmt);
144 +       crashlog_buf->len += vsnprintf(
145 +               &crashlog_buf->data[crashlog_buf->len],
146 +               len, fmt, args);
147 +       va_end(args);
148 +}
149 +
150 +static void crashlog_do_dump(struct kmsg_dumper *dumper,
151 +               enum kmsg_dump_reason reason, const char *s1, unsigned long l1,
152 +               const char *s2, unsigned long l2)
153 +{
154 +       unsigned long s1_start, s2_start;
155 +       unsigned long l1_cpy, l2_cpy;
156 +       struct timeval tv;
157 +       struct module *m;
158 +       char *buf;
159 +       int len;
160 +
161 +       if (!first)
162 +               crashlog_printf("\n===================================\n");
163 +
164 +       do_gettimeofday(&tv);
165 +       crashlog_printf("Time: %lu.%lu\n",
166 +               (long)tv.tv_sec, (long)tv.tv_usec);
167 +
168 +       if (first) {
169 +               crashlog_printf("Modules:");
170 +               list_for_each_entry(m, crashlog_modules, list) {
171 +                       crashlog_printf("\t%s@%p+%x", m->name,
172 +                       m->module_core, m->core_size,
173 +                       m->module_init, m->init_size);
174 +               }
175 +               crashlog_printf("\n");
176 +               first = false;
177 +       }
178 +
179 +       buf = (char *)&crashlog_buf->data[crashlog_buf->len];
180 +       len = get_maxlen();
181 +
182 +       l2_cpy = min(l2, (unsigned long)len);
183 +       l1_cpy = min(l1, (unsigned long)len - l2_cpy);
184 +
185 +       s2_start = l2 - l2_cpy;
186 +       s1_start = l1 - l1_cpy;
187 +
188 +       memcpy(buf, s1 + s1_start, l1_cpy);
189 +       memcpy(buf + l1_cpy, s2 + s2_start, l2_cpy);
190 +       crashlog_buf->len += l1_cpy + l2_cpy;
191 +}
192 +
193 +
194 +int __init crashlog_init_fs(void)
195 +{
196 +       if (!crashlog_addr)
197 +               return -ENOMEM;
198 +
199 +       crashlog_buf = ioremap(crashlog_addr, CRASHLOG_SIZE);
200 +
201 +       crashlog_copy();
202 +
203 +       crashlog_buf->magic = CRASHLOG_MAGIC;
204 +       crashlog_buf->len = 0;
205 +
206 +       dump.dump = crashlog_do_dump;
207 +       kmsg_dump_register(&dump);
208 +
209 +       return 0;
210 +}
211 +module_init(crashlog_init_fs);
212 --- a/mm/bootmem.c
213 +++ b/mm/bootmem.c
214 @@ -15,6 +15,7 @@
215  #include <linux/module.h>
216  #include <linux/kmemleak.h>
217  #include <linux/range.h>
218 +#include <linux/crashlog.h>
219  
220  #include <asm/bug.h>
221  #include <asm/io.h>
222 @@ -226,6 +227,7 @@ static unsigned long __init free_all_boo
223         if (!bdata->node_bootmem_map)
224                 return 0;
225  
226 +       crashlog_init_mem(bdata);
227         start = bdata->node_min_pfn;
228         end = bdata->node_low_pfn;
229  
230 --- a/kernel/module.c
231 +++ b/kernel/module.c
232 @@ -84,6 +84,9 @@ static LIST_HEAD(modules);
233  #ifdef CONFIG_KGDB_KDB
234  struct list_head *kdb_modules = &modules; /* kdb needs the list of modules */
235  #endif /* CONFIG_KGDB_KDB */
236 +#ifdef CONFIG_CRASHLOG
237 +struct list_head *crashlog_modules = &modules;
238 +#endif
239  
240  
241  /* Block module loading/unloading? */