2 * RAM Oops/Panic logger
4 * Copyright (C) 2010 Marco Stornelli <marco.stornelli@gmail.com>
5 * Copyright (C) 2011 Kees Cook <keescook@chromium.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/kernel.h>
26 #include <linux/err.h>
27 #include <linux/module.h>
28 #include <linux/pstore.h>
29 #include <linux/time.h>
31 #include <linux/ioport.h>
32 #include <linux/platform_device.h>
33 #include <linux/slab.h>
34 #include <linux/ramoops.h>
36 #define RAMOOPS_KERNMSG_HDR "===="
37 #define MIN_MEM_SIZE 4096UL
39 static ulong record_size
= MIN_MEM_SIZE
;
40 module_param(record_size
, ulong
, 0400);
41 MODULE_PARM_DESC(record_size
,
42 "size of each dump done on oops/panic");
44 static ulong mem_address
;
45 module_param(mem_address
, ulong
, 0400);
46 MODULE_PARM_DESC(mem_address
,
47 "start of reserved RAM used to store oops/panic logs");
49 static ulong mem_size
;
50 module_param(mem_size
, ulong
, 0400);
51 MODULE_PARM_DESC(mem_size
,
52 "size of reserved RAM used to store oops/panic logs");
54 static int dump_oops
= 1;
55 module_param(dump_oops
, int, 0600);
56 MODULE_PARM_DESC(dump_oops
,
57 "set to 1 to dump oopses, 0 to only dump panics (default 1)");
59 struct ramoops_context
{
61 phys_addr_t phys_addr
;
66 unsigned int max_count
;
67 unsigned int read_count
;
68 struct pstore_info pstore
;
71 static struct platform_device
*dummy
;
72 static struct ramoops_platform_data
*dummy_data
;
74 static int ramoops_pstore_open(struct pstore_info
*psi
)
76 struct ramoops_context
*cxt
= psi
->data
;
82 static ssize_t
ramoops_pstore_read(u64
*id
, enum pstore_type_id
*type
,
83 struct timespec
*time
,
85 struct pstore_info
*psi
)
89 struct ramoops_context
*cxt
= psi
->data
;
91 if (cxt
->read_count
>= cxt
->max_count
)
93 *id
= cxt
->read_count
++;
94 /* Only supports dmesg output so far. */
95 *type
= PSTORE_TYPE_DMESG
;
96 /* TODO(kees): Bogus time for the moment. */
100 rambuf
= cxt
->virt_addr
+ (*id
* cxt
->record_size
);
101 size
= strnlen(rambuf
, cxt
->record_size
);
102 *buf
= kmalloc(size
, GFP_KERNEL
);
105 memcpy(*buf
, rambuf
, size
);
110 static int ramoops_pstore_write(enum pstore_type_id type
,
111 enum kmsg_dump_reason reason
,
114 size_t size
, struct pstore_info
*psi
)
118 struct timeval timestamp
;
119 struct ramoops_context
*cxt
= psi
->data
;
120 size_t available
= cxt
->record_size
;
122 /* Currently ramoops is designed to only store dmesg dumps. */
123 if (type
!= PSTORE_TYPE_DMESG
)
126 /* Out of the various dmesg dump types, ramoops is currently designed
127 * to only store crash logs, rather than storing general kernel logs.
129 if (reason
!= KMSG_DUMP_OOPS
&&
130 reason
!= KMSG_DUMP_PANIC
)
133 /* Skip Oopes when configured to do so. */
134 if (reason
== KMSG_DUMP_OOPS
&& !cxt
->dump_oops
)
137 /* Explicitly only take the first part of any new crash.
138 * If our buffer is larger than kmsg_bytes, this can never happen,
139 * and if our buffer is smaller than kmsg_bytes, we don't want the
140 * report split across multiple records.
145 buf
= cxt
->virt_addr
+ (cxt
->count
* cxt
->record_size
);
147 res
= sprintf(buf
, "%s", RAMOOPS_KERNMSG_HDR
);
151 do_gettimeofday(×tamp
);
152 res
= sprintf(buf
, "%lu.%lu\n", (long)timestamp
.tv_sec
, (long)timestamp
.tv_usec
);
156 if (size
> available
)
159 memcpy(buf
, cxt
->pstore
.buf
, size
);
160 memset(buf
+ size
, '\0', available
- size
);
162 cxt
->count
= (cxt
->count
+ 1) % cxt
->max_count
;
167 static int ramoops_pstore_erase(enum pstore_type_id type
, u64 id
,
168 struct pstore_info
*psi
)
171 struct ramoops_context
*cxt
= psi
->data
;
173 if (id
>= cxt
->max_count
)
176 buf
= cxt
->virt_addr
+ (id
* cxt
->record_size
);
177 memset(buf
, '\0', cxt
->record_size
);
182 static struct ramoops_context oops_cxt
= {
184 .owner
= THIS_MODULE
,
186 .open
= ramoops_pstore_open
,
187 .read
= ramoops_pstore_read
,
188 .write
= ramoops_pstore_write
,
189 .erase
= ramoops_pstore_erase
,
193 static int __init
ramoops_probe(struct platform_device
*pdev
)
195 struct ramoops_platform_data
*pdata
= pdev
->dev
.platform_data
;
196 struct ramoops_context
*cxt
= &oops_cxt
;
199 /* Only a single ramoops area allowed at a time, so fail extra
205 if (!pdata
->mem_size
|| !pdata
->record_size
) {
206 pr_err("The memory size and the record size must be "
211 pdata
->mem_size
= rounddown_pow_of_two(pdata
->mem_size
);
212 pdata
->record_size
= rounddown_pow_of_two(pdata
->record_size
);
214 /* Check for the minimum memory size */
215 if (pdata
->mem_size
< MIN_MEM_SIZE
&&
216 pdata
->record_size
< MIN_MEM_SIZE
) {
217 pr_err("memory size too small, minimum is %lu\n",
222 if (pdata
->mem_size
< pdata
->record_size
) {
223 pr_err("The memory size must be larger than the "
228 cxt
->max_count
= pdata
->mem_size
/ pdata
->record_size
;
230 cxt
->size
= pdata
->mem_size
;
231 cxt
->phys_addr
= pdata
->mem_address
;
232 cxt
->record_size
= pdata
->record_size
;
233 cxt
->dump_oops
= pdata
->dump_oops
;
235 cxt
->pstore
.data
= cxt
;
236 cxt
->pstore
.bufsize
= cxt
->record_size
;
237 cxt
->pstore
.buf
= kmalloc(cxt
->pstore
.bufsize
, GFP_KERNEL
);
238 spin_lock_init(&cxt
->pstore
.buf_lock
);
239 if (!cxt
->pstore
.buf
) {
240 pr_err("cannot allocate pstore buffer\n");
244 if (!request_mem_region(cxt
->phys_addr
, cxt
->size
, "ramoops")) {
245 pr_err("request mem region (0x%lx@0x%llx) failed\n",
246 cxt
->size
, (unsigned long long)cxt
->phys_addr
);
251 cxt
->virt_addr
= ioremap(cxt
->phys_addr
, cxt
->size
);
252 if (!cxt
->virt_addr
) {
253 pr_err("ioremap failed\n");
254 goto fail_mem_region
;
257 err
= pstore_register(&cxt
->pstore
);
259 pr_err("registering with pstore failed\n");
264 * Update the module parameter variables as well so they are visible
265 * through /sys/module/ramoops/parameters/
267 mem_size
= pdata
->mem_size
;
268 mem_address
= pdata
->mem_address
;
269 record_size
= pdata
->record_size
;
270 dump_oops
= pdata
->dump_oops
;
272 pr_info("attached 0x%lx@0x%llx (%ux0x%zx)\n",
273 cxt
->size
, (unsigned long long)cxt
->phys_addr
,
274 cxt
->max_count
, cxt
->record_size
);
279 iounmap(cxt
->virt_addr
);
281 release_mem_region(cxt
->phys_addr
, cxt
->size
);
283 kfree(cxt
->pstore
.buf
);
285 cxt
->pstore
.bufsize
= 0;
291 static int __exit
ramoops_remove(struct platform_device
*pdev
)
294 /* TODO(kees): We cannot unload ramoops since pstore doesn't support
297 struct ramoops_context
*cxt
= &oops_cxt
;
299 iounmap(cxt
->virt_addr
);
300 release_mem_region(cxt
->phys_addr
, cxt
->size
);
303 /* TODO(kees): When pstore supports unregistering, call it here. */
304 kfree(cxt
->pstore
.buf
);
305 cxt
->pstore
.bufsize
= 0;
312 static struct platform_driver ramoops_driver
= {
313 .remove
= __exit_p(ramoops_remove
),
316 .owner
= THIS_MODULE
,
320 static int __init
ramoops_init(void)
323 ret
= platform_driver_probe(&ramoops_driver
, ramoops_probe
);
324 if (ret
== -ENODEV
) {
326 * If we didn't find a platform device, we use module parameters
327 * building platform data on the fly.
329 pr_info("platform device not found, using module parameters\n");
330 dummy_data
= kzalloc(sizeof(struct ramoops_platform_data
),
334 dummy_data
->mem_size
= mem_size
;
335 dummy_data
->mem_address
= mem_address
;
336 dummy_data
->record_size
= record_size
;
337 dummy_data
->dump_oops
= dump_oops
;
338 dummy
= platform_create_bundle(&ramoops_driver
, ramoops_probe
,
340 sizeof(struct ramoops_platform_data
));
343 ret
= PTR_ERR(dummy
);
351 static void __exit
ramoops_exit(void)
353 platform_driver_unregister(&ramoops_driver
);
357 module_init(ramoops_init
);
358 module_exit(ramoops_exit
);
360 MODULE_LICENSE("GPL");
361 MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
362 MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");