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/pstore_ram.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 ramoops_console_size = MIN_MEM_SIZE;
45 module_param_named(console_size, ramoops_console_size, ulong, 0400);
46 MODULE_PARM_DESC(console_size, "size of kernel console log");
48 static ulong mem_address;
49 module_param(mem_address, ulong, 0400);
50 MODULE_PARM_DESC(mem_address,
51 "start of reserved RAM used to store oops/panic logs");
53 static ulong mem_size;
54 module_param(mem_size, ulong, 0400);
55 MODULE_PARM_DESC(mem_size,
56 "size of reserved RAM used to store oops/panic logs");
58 static int dump_oops = 1;
59 module_param(dump_oops, int, 0600);
60 MODULE_PARM_DESC(dump_oops,
61 "set to 1 to dump oopses, 0 to only dump panics (default 1)");
63 static int ramoops_ecc;
64 module_param_named(ecc, ramoops_ecc, int, 0600);
65 MODULE_PARM_DESC(ramoops_ecc,
66 "set to 1 to enable ECC support");
68 struct ramoops_context {
69 struct persistent_ram_zone **przs;
70 struct persistent_ram_zone *cprz;
71 phys_addr_t phys_addr;
77 unsigned int max_dump_cnt;
78 unsigned int dump_write_cnt;
79 unsigned int dump_read_cnt;
80 unsigned int console_read_cnt;
81 struct pstore_info pstore;
84 static struct platform_device *dummy;
85 static struct ramoops_platform_data *dummy_data;
87 static int ramoops_pstore_open(struct pstore_info *psi)
89 struct ramoops_context *cxt = psi->data;
91 cxt->dump_read_cnt = 0;
92 cxt->console_read_cnt = 0;
96 static struct persistent_ram_zone *
97 ramoops_get_next_prz(struct persistent_ram_zone *przs[], uint *c, uint max,
99 enum pstore_type_id *typep, enum pstore_type_id type,
102 struct persistent_ram_zone *prz;
111 /* Update old/shadowed buffer. */
112 persistent_ram_save_old(prz);
113 if (!persistent_ram_old_size(prz))
123 static ssize_t ramoops_pstore_read(u64 *id, enum pstore_type_id *type,
124 struct timespec *time,
126 struct pstore_info *psi)
129 struct ramoops_context *cxt = psi->data;
130 struct persistent_ram_zone *prz;
132 prz = ramoops_get_next_prz(cxt->przs, &cxt->dump_read_cnt,
133 cxt->max_dump_cnt, id, type,
134 PSTORE_TYPE_DMESG, 1);
136 prz = ramoops_get_next_prz(&cxt->cprz, &cxt->console_read_cnt,
137 1, id, type, PSTORE_TYPE_CONSOLE, 0);
141 /* TODO(kees): Bogus time for the moment. */
145 size = persistent_ram_old_size(prz);
146 *buf = kmalloc(size, GFP_KERNEL);
149 memcpy(*buf, persistent_ram_old(prz), size);
154 static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz)
157 struct timeval timestamp;
160 do_gettimeofday(×tamp);
161 hdr = kasprintf(GFP_ATOMIC, RAMOOPS_KERNMSG_HDR "%lu.%lu\n",
162 (long)timestamp.tv_sec, (long)timestamp.tv_usec);
164 len = hdr ? strlen(hdr) : 0;
165 persistent_ram_write(prz, hdr, len);
171 static int ramoops_pstore_write(enum pstore_type_id type,
172 enum kmsg_dump_reason reason,
175 size_t size, struct pstore_info *psi)
177 struct ramoops_context *cxt = psi->data;
178 struct persistent_ram_zone *prz = cxt->przs[cxt->dump_write_cnt];
181 if (type == PSTORE_TYPE_CONSOLE) {
184 persistent_ram_write(cxt->cprz, cxt->pstore.buf, size);
188 if (type != PSTORE_TYPE_DMESG)
191 /* Out of the various dmesg dump types, ramoops is currently designed
192 * to only store crash logs, rather than storing general kernel logs.
194 if (reason != KMSG_DUMP_OOPS &&
195 reason != KMSG_DUMP_PANIC)
198 /* Skip Oopes when configured to do so. */
199 if (reason == KMSG_DUMP_OOPS && !cxt->dump_oops)
202 /* Explicitly only take the first part of any new crash.
203 * If our buffer is larger than kmsg_bytes, this can never happen,
204 * and if our buffer is smaller than kmsg_bytes, we don't want the
205 * report split across multiple records.
210 hlen = ramoops_write_kmsg_hdr(prz);
211 if (size + hlen > prz->buffer_size)
212 size = prz->buffer_size - hlen;
213 persistent_ram_write(prz, cxt->pstore.buf, size);
215 cxt->dump_write_cnt = (cxt->dump_write_cnt + 1) % cxt->max_dump_cnt;
220 static int ramoops_pstore_erase(enum pstore_type_id type, u64 id,
221 struct pstore_info *psi)
223 struct ramoops_context *cxt = psi->data;
224 struct persistent_ram_zone *prz;
227 case PSTORE_TYPE_DMESG:
228 if (id >= cxt->max_dump_cnt)
232 case PSTORE_TYPE_CONSOLE:
239 persistent_ram_free_old(prz);
240 persistent_ram_zap(prz);
245 static struct ramoops_context oops_cxt = {
247 .owner = THIS_MODULE,
249 .open = ramoops_pstore_open,
250 .read = ramoops_pstore_read,
251 .write = ramoops_pstore_write,
252 .erase = ramoops_pstore_erase,
256 static void ramoops_free_przs(struct ramoops_context *cxt)
263 for (i = 0; cxt->przs[i]; i++)
264 persistent_ram_free(cxt->przs[i]);
268 static int ramoops_init_przs(struct device *dev, struct ramoops_context *cxt,
269 phys_addr_t *paddr, size_t dump_mem_sz)
274 if (!cxt->record_size)
277 cxt->max_dump_cnt = dump_mem_sz / cxt->record_size;
278 if (!cxt->max_dump_cnt)
281 cxt->przs = kzalloc(sizeof(*cxt->przs) * cxt->max_dump_cnt,
284 dev_err(dev, "failed to initialize a prz array for dumps\n");
288 for (i = 0; i < cxt->max_dump_cnt; i++) {
289 size_t sz = cxt->record_size;
291 cxt->przs[i] = persistent_ram_new(*paddr, sz, cxt->ecc);
292 if (IS_ERR(cxt->przs[i])) {
293 err = PTR_ERR(cxt->przs[i]);
294 dev_err(dev, "failed to request mem region (0x%zx@0x%llx): %d\n",
295 sz, (unsigned long long)*paddr, err);
303 ramoops_free_przs(cxt);
307 static int ramoops_init_prz(struct device *dev, struct ramoops_context *cxt,
308 struct persistent_ram_zone **prz,
309 phys_addr_t *paddr, size_t sz)
314 if (*paddr + sz > *paddr + cxt->size)
317 *prz = persistent_ram_new(*paddr, sz, cxt->ecc);
319 int err = PTR_ERR(*prz);
321 dev_err(dev, "failed to request mem region (0x%zx@0x%llx): %d\n",
322 sz, (unsigned long long)*paddr, err);
326 persistent_ram_zap(*prz);
333 static int __init ramoops_probe(struct platform_device *pdev)
335 struct device *dev = &pdev->dev;
336 struct ramoops_platform_data *pdata = pdev->dev.platform_data;
337 struct ramoops_context *cxt = &oops_cxt;
342 /* Only a single ramoops area allowed at a time, so fail extra
345 if (cxt->max_dump_cnt)
348 if (!pdata->mem_size || (!pdata->record_size && !pdata->console_size)) {
349 pr_err("The memory size and the record/console size must be "
354 pdata->mem_size = rounddown_pow_of_two(pdata->mem_size);
355 pdata->record_size = rounddown_pow_of_two(pdata->record_size);
356 pdata->console_size = rounddown_pow_of_two(pdata->console_size);
358 cxt->dump_read_cnt = 0;
359 cxt->size = pdata->mem_size;
360 cxt->phys_addr = pdata->mem_address;
361 cxt->record_size = pdata->record_size;
362 cxt->console_size = pdata->console_size;
363 cxt->dump_oops = pdata->dump_oops;
364 cxt->ecc = pdata->ecc;
366 paddr = cxt->phys_addr;
368 dump_mem_sz = cxt->size - cxt->console_size;
369 err = ramoops_init_przs(dev, cxt, &paddr, dump_mem_sz);
373 err = ramoops_init_prz(dev, cxt, &cxt->cprz, &paddr, cxt->console_size);
377 if (!cxt->przs && !cxt->cprz) {
378 pr_err("memory size too small, minimum is %lu\n",
379 cxt->console_size + cxt->record_size);
383 cxt->pstore.data = cxt;
385 * Console can handle any buffer size, so prefer dumps buffer
386 * size since usually it is smaller.
389 cxt->pstore.bufsize = cxt->przs[0]->buffer_size;
391 cxt->pstore.bufsize = cxt->cprz->buffer_size;
392 cxt->pstore.buf = kmalloc(cxt->pstore.bufsize, GFP_KERNEL);
393 spin_lock_init(&cxt->pstore.buf_lock);
394 if (!cxt->pstore.buf) {
395 pr_err("cannot allocate pstore buffer\n");
399 err = pstore_register(&cxt->pstore);
401 pr_err("registering with pstore failed\n");
406 * Update the module parameter variables as well so they are visible
407 * through /sys/module/ramoops/parameters/
409 mem_size = pdata->mem_size;
410 mem_address = pdata->mem_address;
411 record_size = pdata->record_size;
412 dump_oops = pdata->dump_oops;
414 pr_info("attached 0x%lx@0x%llx, ecc: %s\n",
415 cxt->size, (unsigned long long)cxt->phys_addr,
416 ramoops_ecc ? "on" : "off");
421 kfree(cxt->pstore.buf);
423 cxt->pstore.bufsize = 0;
424 cxt->max_dump_cnt = 0;
428 ramoops_free_przs(cxt);
433 static int __exit ramoops_remove(struct platform_device *pdev)
436 /* TODO(kees): We cannot unload ramoops since pstore doesn't support
439 struct ramoops_context *cxt = &oops_cxt;
441 iounmap(cxt->virt_addr);
442 release_mem_region(cxt->phys_addr, cxt->size);
443 cxt->max_dump_cnt = 0;
445 /* TODO(kees): When pstore supports unregistering, call it here. */
446 kfree(cxt->pstore.buf);
447 cxt->pstore.bufsize = 0;
454 static struct platform_driver ramoops_driver = {
455 .remove = __exit_p(ramoops_remove),
458 .owner = THIS_MODULE,
462 static int __init ramoops_init(void)
465 ret = platform_driver_probe(&ramoops_driver, ramoops_probe);
466 if (ret == -ENODEV) {
468 * If we didn't find a platform device, we use module parameters
469 * building platform data on the fly.
471 pr_info("platform device not found, using module parameters\n");
472 dummy_data = kzalloc(sizeof(struct ramoops_platform_data),
476 dummy_data->mem_size = mem_size;
477 dummy_data->mem_address = mem_address;
478 dummy_data->record_size = record_size;
479 dummy_data->console_size = ramoops_console_size;
480 dummy_data->dump_oops = dump_oops;
481 dummy_data->ecc = ramoops_ecc;
482 dummy = platform_create_bundle(&ramoops_driver, ramoops_probe,
484 sizeof(struct ramoops_platform_data));
487 ret = PTR_ERR(dummy);
495 static void __exit ramoops_exit(void)
497 platform_driver_unregister(&ramoops_driver);
501 module_init(ramoops_init);
502 module_exit(ramoops_exit);
504 MODULE_LICENSE("GPL");
505 MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
506 MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");