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