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 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 static int ramoops_ecc;
60 module_param_named(ecc, ramoops_ecc, int, 0600);
61 MODULE_PARM_DESC(ramoops_ecc,
62 "set to 1 to enable ECC support");
64 struct ramoops_context {
65 struct persistent_ram_zone **przs;
66 phys_addr_t phys_addr;
72 unsigned int max_count;
73 unsigned int read_count;
74 struct pstore_info pstore;
77 static struct platform_device *dummy;
78 static struct ramoops_platform_data *dummy_data;
80 static int ramoops_pstore_open(struct pstore_info *psi)
82 struct ramoops_context *cxt = psi->data;
88 static ssize_t ramoops_pstore_read(u64 *id, enum pstore_type_id *type,
89 struct timespec *time,
91 struct pstore_info *psi)
94 struct ramoops_context *cxt = psi->data;
95 struct persistent_ram_zone *prz;
97 if (cxt->read_count >= cxt->max_count)
100 *id = cxt->read_count++;
101 prz = cxt->przs[*id];
103 /* Only supports dmesg output so far. */
104 *type = PSTORE_TYPE_DMESG;
105 /* TODO(kees): Bogus time for the moment. */
109 /* Update old/shadowed buffer. */
110 persistent_ram_save_old(prz);
111 size = persistent_ram_old_size(prz);
112 *buf = kmalloc(size, GFP_KERNEL);
115 memcpy(*buf, persistent_ram_old(prz), size);
120 static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz)
123 struct timeval timestamp;
126 do_gettimeofday(×tamp);
127 hdr = kasprintf(GFP_ATOMIC, RAMOOPS_KERNMSG_HDR "%lu.%lu\n",
128 (long)timestamp.tv_sec, (long)timestamp.tv_usec);
130 len = hdr ? strlen(hdr) : 0;
131 persistent_ram_write(prz, hdr, len);
137 static int ramoops_pstore_write(enum pstore_type_id type,
138 enum kmsg_dump_reason reason,
141 size_t size, struct pstore_info *psi)
143 struct ramoops_context *cxt = psi->data;
144 struct persistent_ram_zone *prz = cxt->przs[cxt->count];
147 /* Currently ramoops is designed to only store dmesg dumps. */
148 if (type != PSTORE_TYPE_DMESG)
151 /* Out of the various dmesg dump types, ramoops is currently designed
152 * to only store crash logs, rather than storing general kernel logs.
154 if (reason != KMSG_DUMP_OOPS &&
155 reason != KMSG_DUMP_PANIC)
158 /* Skip Oopes when configured to do so. */
159 if (reason == KMSG_DUMP_OOPS && !cxt->dump_oops)
162 /* Explicitly only take the first part of any new crash.
163 * If our buffer is larger than kmsg_bytes, this can never happen,
164 * and if our buffer is smaller than kmsg_bytes, we don't want the
165 * report split across multiple records.
170 hlen = ramoops_write_kmsg_hdr(prz);
171 if (size + hlen > prz->buffer_size)
172 size = prz->buffer_size - hlen;
173 persistent_ram_write(prz, cxt->pstore.buf, size);
175 cxt->count = (cxt->count + 1) % cxt->max_count;
180 static int ramoops_pstore_erase(enum pstore_type_id type, u64 id,
181 struct pstore_info *psi)
183 struct ramoops_context *cxt = psi->data;
185 if (id >= cxt->max_count)
188 persistent_ram_free_old(cxt->przs[id]);
193 static struct ramoops_context oops_cxt = {
195 .owner = THIS_MODULE,
197 .open = ramoops_pstore_open,
198 .read = ramoops_pstore_read,
199 .write = ramoops_pstore_write,
200 .erase = ramoops_pstore_erase,
204 static int __init ramoops_probe(struct platform_device *pdev)
206 struct device *dev = &pdev->dev;
207 struct ramoops_platform_data *pdata = pdev->dev.platform_data;
208 struct ramoops_context *cxt = &oops_cxt;
212 /* Only a single ramoops area allowed at a time, so fail extra
218 if (!pdata->mem_size || !pdata->record_size) {
219 pr_err("The memory size and the record size must be "
224 pdata->mem_size = rounddown_pow_of_two(pdata->mem_size);
225 pdata->record_size = rounddown_pow_of_two(pdata->record_size);
227 /* Check for the minimum memory size */
228 if (pdata->mem_size < MIN_MEM_SIZE &&
229 pdata->record_size < MIN_MEM_SIZE) {
230 pr_err("memory size too small, minimum is %lu\n",
235 if (pdata->mem_size < pdata->record_size) {
236 pr_err("The memory size must be larger than the "
241 cxt->max_count = pdata->mem_size / pdata->record_size;
243 cxt->size = pdata->mem_size;
244 cxt->phys_addr = pdata->mem_address;
245 cxt->record_size = pdata->record_size;
246 cxt->dump_oops = pdata->dump_oops;
247 cxt->ecc = pdata->ecc;
249 cxt->przs = kzalloc(sizeof(*cxt->przs) * cxt->max_count, GFP_KERNEL);
252 dev_err(dev, "failed to initialize a prz array\n");
256 for (i = 0; i < cxt->max_count; i++) {
257 size_t sz = cxt->record_size;
258 phys_addr_t start = cxt->phys_addr + sz * i;
260 cxt->przs[i] = persistent_ram_new(start, sz, cxt->ecc);
261 if (IS_ERR(cxt->przs[i])) {
262 err = PTR_ERR(cxt->przs[i]);
263 dev_err(dev, "failed to request mem region (0x%zx@0x%llx): %d\n",
264 sz, (unsigned long long)start, err);
269 cxt->pstore.data = cxt;
270 cxt->pstore.bufsize = cxt->przs[0]->buffer_size;
271 cxt->pstore.buf = kmalloc(cxt->pstore.bufsize, GFP_KERNEL);
272 spin_lock_init(&cxt->pstore.buf_lock);
273 if (!cxt->pstore.buf) {
274 pr_err("cannot allocate pstore buffer\n");
278 err = pstore_register(&cxt->pstore);
280 pr_err("registering with pstore failed\n");
285 * Update the module parameter variables as well so they are visible
286 * through /sys/module/ramoops/parameters/
288 mem_size = pdata->mem_size;
289 mem_address = pdata->mem_address;
290 record_size = pdata->record_size;
291 dump_oops = pdata->dump_oops;
293 pr_info("attached 0x%lx@0x%llx (%ux0x%zx), ecc: %s\n",
294 cxt->size, (unsigned long long)cxt->phys_addr,
295 cxt->max_count, cxt->record_size,
296 ramoops_ecc ? "on" : "off");
301 kfree(cxt->pstore.buf);
303 cxt->pstore.bufsize = 0;
306 for (i = 0; cxt->przs[i]; i++)
307 persistent_ram_free(cxt->przs[i]);
313 static int __exit ramoops_remove(struct platform_device *pdev)
316 /* TODO(kees): We cannot unload ramoops since pstore doesn't support
319 struct ramoops_context *cxt = &oops_cxt;
321 iounmap(cxt->virt_addr);
322 release_mem_region(cxt->phys_addr, cxt->size);
325 /* TODO(kees): When pstore supports unregistering, call it here. */
326 kfree(cxt->pstore.buf);
327 cxt->pstore.bufsize = 0;
334 static struct platform_driver ramoops_driver = {
335 .remove = __exit_p(ramoops_remove),
338 .owner = THIS_MODULE,
342 static int __init ramoops_init(void)
345 ret = platform_driver_probe(&ramoops_driver, ramoops_probe);
346 if (ret == -ENODEV) {
348 * If we didn't find a platform device, we use module parameters
349 * building platform data on the fly.
351 pr_info("platform device not found, using module parameters\n");
352 dummy_data = kzalloc(sizeof(struct ramoops_platform_data),
356 dummy_data->mem_size = mem_size;
357 dummy_data->mem_address = mem_address;
358 dummy_data->record_size = record_size;
359 dummy_data->dump_oops = dump_oops;
360 dummy_data->ecc = ramoops_ecc;
361 dummy = platform_create_bundle(&ramoops_driver, ramoops_probe,
363 sizeof(struct ramoops_platform_data));
366 ret = PTR_ERR(dummy);
374 static void __exit ramoops_exit(void)
376 platform_driver_unregister(&ramoops_driver);
380 module_init(ramoops_init);
381 module_exit(ramoops_exit);
383 MODULE_LICENSE("GPL");
384 MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
385 MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");