1/* 2 * RAM Oops/Panic logger 3 * 4 * Copyright (C) 2010 Marco Stornelli <marco.stornelli@gmail.com> 5 * 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. 9 * 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. 14 * 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 18 * 02110-1301 USA 19 * 20 */ 21 22#include <linux/kernel.h> 23#include <linux/module.h> 24#include <linux/kmsg_dump.h> 25#include <linux/time.h> 26#include <linux/io.h> 27#include <linux/ioport.h> 28 29#define RAMOOPS_KERNMSG_HDR "====" 30 31#define RECORD_SIZE 4096 32 33static ulong mem_address; 34module_param(mem_address, ulong, 0400); 35MODULE_PARM_DESC(mem_address, 36 "start of reserved RAM used to store oops/panic logs"); 37 38static ulong mem_size; 39module_param(mem_size, ulong, 0400); 40MODULE_PARM_DESC(mem_size, 41 "size of reserved RAM used to store oops/panic logs"); 42 43static int dump_oops = 1; 44module_param(dump_oops, int, 0600); 45MODULE_PARM_DESC(dump_oops, 46 "set to 1 to dump oopses, 0 to only dump panics (default 1)"); 47 48static struct ramoops_context { 49 struct kmsg_dumper dump; 50 void *virt_addr; 51 phys_addr_t phys_addr; 52 unsigned long size; 53 int count; 54 int max_count; 55} oops_cxt; 56 57static void ramoops_do_dump(struct kmsg_dumper *dumper, 58 enum kmsg_dump_reason reason, const char *s1, unsigned long l1, 59 const char *s2, unsigned long l2) 60{ 61 struct ramoops_context *cxt = container_of(dumper, 62 struct ramoops_context, dump); 63 unsigned long s1_start, s2_start; 64 unsigned long l1_cpy, l2_cpy; 65 int res, hdr_size; 66 char *buf, *buf_orig; 67 struct timeval timestamp; 68 69 /* Only dump oopses if dump_oops is set */ 70 if (reason == KMSG_DUMP_OOPS && !dump_oops) 71 return; 72 73 buf = (char *)(cxt->virt_addr + (cxt->count * RECORD_SIZE)); 74 buf_orig = buf; 75 76 memset(buf, '\0', RECORD_SIZE); 77 res = sprintf(buf, "%s", RAMOOPS_KERNMSG_HDR); 78 buf += res; 79 do_gettimeofday(×tamp); 80 res = sprintf(buf, "%lu.%lu\n", (long)timestamp.tv_sec, (long)timestamp.tv_usec); 81 buf += res; 82 83 hdr_size = buf - buf_orig; 84 l2_cpy = min(l2, (unsigned long)(RECORD_SIZE - hdr_size)); 85 l1_cpy = min(l1, (unsigned long)(RECORD_SIZE - hdr_size) - l2_cpy); 86 87 s2_start = l2 - l2_cpy; 88 s1_start = l1 - l1_cpy; 89 90 memcpy(buf, s1 + s1_start, l1_cpy); 91 memcpy(buf + l1_cpy, s2 + s2_start, l2_cpy); 92 93 cxt->count = (cxt->count + 1) % cxt->max_count; 94} 95 96static int __init ramoops_init(void) 97{ 98 struct ramoops_context *cxt = &oops_cxt; 99 int err = -EINVAL; 100 101 if (!mem_size) { 102 printk(KERN_ERR "ramoops: invalid size specification"); 103 goto fail3; 104 } 105 106 rounddown_pow_of_two(mem_size); 107 108 if (mem_size < RECORD_SIZE) { 109 printk(KERN_ERR "ramoops: size too small"); 110 goto fail3; 111 } 112 113 cxt->max_count = mem_size / RECORD_SIZE; 114 cxt->count = 0; 115 cxt->size = mem_size; 116 cxt->phys_addr = mem_address; 117 118 if (!request_mem_region(cxt->phys_addr, cxt->size, "ramoops")) { 119 printk(KERN_ERR "ramoops: request mem region failed"); 120 err = -EINVAL; 121 goto fail3; 122 } 123 124 cxt->virt_addr = ioremap(cxt->phys_addr, cxt->size); 125 if (!cxt->virt_addr) { 126 printk(KERN_ERR "ramoops: ioremap failed"); 127 goto fail2; 128 } 129 130 cxt->dump.dump = ramoops_do_dump; 131 err = kmsg_dump_register(&cxt->dump); 132 if (err) { 133 printk(KERN_ERR "ramoops: registering kmsg dumper failed"); 134 goto fail1; 135 } 136 137 return 0; 138 139fail1: 140 iounmap(cxt->virt_addr); 141fail2: 142 release_mem_region(cxt->phys_addr, cxt->size); 143fail3: 144 return err; 145} 146 147static void __exit ramoops_exit(void) 148{ 149 struct ramoops_context *cxt = &oops_cxt; 150 151 if (kmsg_dump_unregister(&cxt->dump) < 0) 152 printk(KERN_WARNING "ramoops: could not unregister kmsg_dumper"); 153 154 iounmap(cxt->virt_addr); 155 release_mem_region(cxt->phys_addr, cxt->size); 156} 157 158 159module_init(ramoops_init); 160module_exit(ramoops_exit); 161 162MODULE_LICENSE("GPL"); 163MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>"); 164MODULE_DESCRIPTION("RAM Oops/Panic logger/driver"); 165