2 * RAM Oops/Panic logger
4 * Copyright (C) 2010 Marco Stornelli <marco.stornelli@gmail.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/kernel.h>
25 #include <linux/err.h>
26 #include <linux/module.h>
27 #include <linux/kmsg_dump.h>
28 #include <linux/time.h>
30 #include <linux/ioport.h>
31 #include <linux/platform_device.h>
32 #include <linux/slab.h>
33 #include <linux/ramoops.h>
35 #define RAMOOPS_KERNMSG_HDR "===="
36 #define MIN_MEM_SIZE 4096UL
38 static ulong record_size
= MIN_MEM_SIZE
;
39 module_param(record_size
, ulong
, 0400);
40 MODULE_PARM_DESC(record_size
,
41 "size of each dump done on oops/panic");
43 static ulong mem_address
;
44 module_param(mem_address
, ulong
, 0400);
45 MODULE_PARM_DESC(mem_address
,
46 "start of reserved RAM used to store oops/panic logs");
48 static ulong mem_size
;
49 module_param(mem_size
, ulong
, 0400);
50 MODULE_PARM_DESC(mem_size
,
51 "size of reserved RAM used to store oops/panic logs");
53 static int dump_oops
= 1;
54 module_param(dump_oops
, int, 0600);
55 MODULE_PARM_DESC(dump_oops
,
56 "set to 1 to dump oopses, 0 to only dump panics (default 1)");
58 static struct ramoops_context
{
59 struct kmsg_dumper dump
;
61 phys_addr_t phys_addr
;
63 unsigned long record_size
;
69 static struct platform_device
*dummy
;
70 static struct ramoops_platform_data
*dummy_data
;
72 static void ramoops_do_dump(struct kmsg_dumper
*dumper
,
73 enum kmsg_dump_reason reason
, const char *s1
, unsigned long l1
,
74 const char *s2
, unsigned long l2
)
76 struct ramoops_context
*cxt
= container_of(dumper
,
77 struct ramoops_context
, dump
);
78 unsigned long s1_start
, s2_start
;
79 unsigned long l1_cpy
, l2_cpy
;
82 struct timeval timestamp
;
84 if (reason
!= KMSG_DUMP_OOPS
&&
85 reason
!= KMSG_DUMP_PANIC
&&
86 reason
!= KMSG_DUMP_KEXEC
)
89 /* Only dump oopses if dump_oops is set */
90 if (reason
== KMSG_DUMP_OOPS
&& !cxt
->dump_oops
)
93 buf
= cxt
->virt_addr
+ (cxt
->count
* cxt
->record_size
);
96 memset(buf
, '\0', cxt
->record_size
);
97 res
= sprintf(buf
, "%s", RAMOOPS_KERNMSG_HDR
);
99 do_gettimeofday(×tamp
);
100 res
= sprintf(buf
, "%lu.%lu\n", (long)timestamp
.tv_sec
, (long)timestamp
.tv_usec
);
103 hdr_size
= buf
- buf_orig
;
104 l2_cpy
= min(l2
, cxt
->record_size
- hdr_size
);
105 l1_cpy
= min(l1
, cxt
->record_size
- hdr_size
- l2_cpy
);
107 s2_start
= l2
- l2_cpy
;
108 s1_start
= l1
- l1_cpy
;
110 memcpy(buf
, s1
+ s1_start
, l1_cpy
);
111 memcpy(buf
+ l1_cpy
, s2
+ s2_start
, l2_cpy
);
113 cxt
->count
= (cxt
->count
+ 1) % cxt
->max_count
;
116 static int __init
ramoops_probe(struct platform_device
*pdev
)
118 struct ramoops_platform_data
*pdata
= pdev
->dev
.platform_data
;
119 struct ramoops_context
*cxt
= &oops_cxt
;
122 if (!pdata
->mem_size
|| !pdata
->record_size
) {
123 pr_err("The memory size and the record size must be "
128 rounddown_pow_of_two(pdata
->mem_size
);
129 rounddown_pow_of_two(pdata
->record_size
);
131 /* Check for the minimum memory size */
132 if (pdata
->mem_size
< MIN_MEM_SIZE
&&
133 pdata
->record_size
< MIN_MEM_SIZE
) {
134 pr_err("memory size too small, minium is %lu\n", MIN_MEM_SIZE
);
138 if (pdata
->mem_size
< pdata
->record_size
) {
139 pr_err("The memory size must be larger than the "
144 cxt
->max_count
= pdata
->mem_size
/ pdata
->record_size
;
146 cxt
->size
= pdata
->mem_size
;
147 cxt
->phys_addr
= pdata
->mem_address
;
148 cxt
->record_size
= pdata
->record_size
;
149 cxt
->dump_oops
= pdata
->dump_oops
;
151 if (!request_mem_region(cxt
->phys_addr
, cxt
->size
, "ramoops")) {
152 pr_err("request mem region failed\n");
157 cxt
->virt_addr
= ioremap(cxt
->phys_addr
, cxt
->size
);
158 if (!cxt
->virt_addr
) {
159 pr_err("ioremap failed\n");
163 cxt
->dump
.dump
= ramoops_do_dump
;
164 err
= kmsg_dump_register(&cxt
->dump
);
166 pr_err("registering kmsg dumper failed\n");
173 iounmap(cxt
->virt_addr
);
175 release_mem_region(cxt
->phys_addr
, cxt
->size
);
180 static int __exit
ramoops_remove(struct platform_device
*pdev
)
182 struct ramoops_context
*cxt
= &oops_cxt
;
184 if (kmsg_dump_unregister(&cxt
->dump
) < 0)
185 pr_warn("could not unregister kmsg_dumper\n");
187 iounmap(cxt
->virt_addr
);
188 release_mem_region(cxt
->phys_addr
, cxt
->size
);
192 static struct platform_driver ramoops_driver
= {
193 .remove
= __exit_p(ramoops_remove
),
196 .owner
= THIS_MODULE
,
200 static int __init
ramoops_init(void)
203 ret
= platform_driver_probe(&ramoops_driver
, ramoops_probe
);
204 if (ret
== -ENODEV
) {
206 * If we didn't find a platform device, we use module parameters
207 * building platform data on the fly.
209 pr_info("platform device not found, using module parameters\n");
210 dummy_data
= kzalloc(sizeof(struct ramoops_platform_data
),
214 dummy_data
->mem_size
= mem_size
;
215 dummy_data
->mem_address
= mem_address
;
216 dummy_data
->record_size
= record_size
;
217 dummy_data
->dump_oops
= dump_oops
;
218 dummy
= platform_create_bundle(&ramoops_driver
, ramoops_probe
,
220 sizeof(struct ramoops_platform_data
));
223 ret
= PTR_ERR(dummy
);
231 static void __exit
ramoops_exit(void)
233 platform_driver_unregister(&ramoops_driver
);
237 module_init(ramoops_init
);
238 module_exit(ramoops_exit
);
240 MODULE_LICENSE("GPL");
241 MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
242 MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");