Commit | Line | Data |
---|---|---|
56d611a0 MS |
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 | ||
0169256e MS |
22 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
23 | ||
56d611a0 | 24 | #include <linux/kernel.h> |
83c1b317 | 25 | #include <linux/err.h> |
56d611a0 MS |
26 | #include <linux/module.h> |
27 | #include <linux/kmsg_dump.h> | |
28 | #include <linux/time.h> | |
29 | #include <linux/io.h> | |
30 | #include <linux/ioport.h> | |
c3b92ce9 | 31 | #include <linux/platform_device.h> |
13aefd72 | 32 | #include <linux/slab.h> |
c3b92ce9 | 33 | #include <linux/ramoops.h> |
56d611a0 MS |
34 | |
35 | #define RAMOOPS_KERNMSG_HDR "====" | |
3e5c4fad | 36 | #define MIN_MEM_SIZE 4096UL |
56d611a0 | 37 | |
3e5c4fad SI |
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"); | |
56d611a0 MS |
42 | |
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"); | |
47 | ||
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"); | |
52 | ||
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)"); | |
57 | ||
58 | static struct ramoops_context { | |
59 | struct kmsg_dumper dump; | |
60 | void *virt_addr; | |
61 | phys_addr_t phys_addr; | |
62 | unsigned long size; | |
3e5c4fad | 63 | unsigned long record_size; |
6b4d2a27 | 64 | int dump_oops; |
56d611a0 MS |
65 | int count; |
66 | int max_count; | |
67 | } oops_cxt; | |
68 | ||
13aefd72 MS |
69 | static struct platform_device *dummy; |
70 | static struct ramoops_platform_data *dummy_data; | |
71 | ||
56d611a0 MS |
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) | |
75 | { | |
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; | |
1873bb81 AD |
80 | int res, hdr_size; |
81 | char *buf, *buf_orig; | |
56d611a0 MS |
82 | struct timeval timestamp; |
83 | ||
fc2d557c SA |
84 | if (reason != KMSG_DUMP_OOPS && |
85 | reason != KMSG_DUMP_PANIC && | |
86 | reason != KMSG_DUMP_KEXEC) | |
87 | return; | |
88 | ||
56d611a0 | 89 | /* Only dump oopses if dump_oops is set */ |
6b4d2a27 | 90 | if (reason == KMSG_DUMP_OOPS && !cxt->dump_oops) |
56d611a0 MS |
91 | return; |
92 | ||
3e5c4fad | 93 | buf = cxt->virt_addr + (cxt->count * cxt->record_size); |
1873bb81 AD |
94 | buf_orig = buf; |
95 | ||
3e5c4fad | 96 | memset(buf, '\0', cxt->record_size); |
56d611a0 MS |
97 | res = sprintf(buf, "%s", RAMOOPS_KERNMSG_HDR); |
98 | buf += res; | |
99 | do_gettimeofday(×tamp); | |
100 | res = sprintf(buf, "%lu.%lu\n", (long)timestamp.tv_sec, (long)timestamp.tv_usec); | |
101 | buf += res; | |
102 | ||
1873bb81 | 103 | hdr_size = buf - buf_orig; |
3e5c4fad SI |
104 | l2_cpy = min(l2, cxt->record_size - hdr_size); |
105 | l1_cpy = min(l1, cxt->record_size - hdr_size - l2_cpy); | |
56d611a0 MS |
106 | |
107 | s2_start = l2 - l2_cpy; | |
108 | s1_start = l1 - l1_cpy; | |
109 | ||
110 | memcpy(buf, s1 + s1_start, l1_cpy); | |
111 | memcpy(buf + l1_cpy, s2 + s2_start, l2_cpy); | |
112 | ||
113 | cxt->count = (cxt->count + 1) % cxt->max_count; | |
114 | } | |
115 | ||
c3b92ce9 | 116 | static int __init ramoops_probe(struct platform_device *pdev) |
56d611a0 | 117 | { |
c3b92ce9 | 118 | struct ramoops_platform_data *pdata = pdev->dev.platform_data; |
56d611a0 MS |
119 | struct ramoops_context *cxt = &oops_cxt; |
120 | int err = -EINVAL; | |
121 | ||
3e5c4fad SI |
122 | if (!pdata->mem_size || !pdata->record_size) { |
123 | pr_err("The memory size and the record size must be " | |
124 | "non-zero\n"); | |
56d611a0 MS |
125 | goto fail3; |
126 | } | |
127 | ||
13aefd72 | 128 | rounddown_pow_of_two(pdata->mem_size); |
3e5c4fad | 129 | rounddown_pow_of_two(pdata->record_size); |
56d611a0 | 130 | |
3e5c4fad SI |
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); | |
56d611a0 MS |
135 | goto fail3; |
136 | } | |
137 | ||
3e5c4fad SI |
138 | if (pdata->mem_size < pdata->record_size) { |
139 | pr_err("The memory size must be larger than the " | |
140 | "records size\n"); | |
141 | goto fail3; | |
142 | } | |
143 | ||
144 | cxt->max_count = pdata->mem_size / pdata->record_size; | |
56d611a0 | 145 | cxt->count = 0; |
13aefd72 MS |
146 | cxt->size = pdata->mem_size; |
147 | cxt->phys_addr = pdata->mem_address; | |
3e5c4fad | 148 | cxt->record_size = pdata->record_size; |
6b4d2a27 | 149 | cxt->dump_oops = pdata->dump_oops; |
f7b9fcbb SI |
150 | /* |
151 | * Update the module parameter variables as well so they are visible | |
152 | * through /sys/module/ramoops/parameters/ | |
153 | */ | |
154 | mem_size = pdata->mem_size; | |
155 | mem_address = pdata->mem_address; | |
156 | record_size = pdata->record_size; | |
157 | dump_oops = pdata->dump_oops; | |
56d611a0 MS |
158 | |
159 | if (!request_mem_region(cxt->phys_addr, cxt->size, "ramoops")) { | |
0169256e | 160 | pr_err("request mem region failed\n"); |
56d611a0 MS |
161 | err = -EINVAL; |
162 | goto fail3; | |
163 | } | |
164 | ||
165 | cxt->virt_addr = ioremap(cxt->phys_addr, cxt->size); | |
166 | if (!cxt->virt_addr) { | |
0169256e | 167 | pr_err("ioremap failed\n"); |
56d611a0 MS |
168 | goto fail2; |
169 | } | |
170 | ||
171 | cxt->dump.dump = ramoops_do_dump; | |
172 | err = kmsg_dump_register(&cxt->dump); | |
173 | if (err) { | |
0169256e | 174 | pr_err("registering kmsg dumper failed\n"); |
56d611a0 MS |
175 | goto fail1; |
176 | } | |
177 | ||
178 | return 0; | |
179 | ||
180 | fail1: | |
181 | iounmap(cxt->virt_addr); | |
182 | fail2: | |
183 | release_mem_region(cxt->phys_addr, cxt->size); | |
184 | fail3: | |
185 | return err; | |
186 | } | |
187 | ||
c3b92ce9 | 188 | static int __exit ramoops_remove(struct platform_device *pdev) |
56d611a0 MS |
189 | { |
190 | struct ramoops_context *cxt = &oops_cxt; | |
191 | ||
192 | if (kmsg_dump_unregister(&cxt->dump) < 0) | |
0169256e | 193 | pr_warn("could not unregister kmsg_dumper\n"); |
56d611a0 MS |
194 | |
195 | iounmap(cxt->virt_addr); | |
196 | release_mem_region(cxt->phys_addr, cxt->size); | |
c3b92ce9 | 197 | return 0; |
56d611a0 MS |
198 | } |
199 | ||
c3b92ce9 KP |
200 | static struct platform_driver ramoops_driver = { |
201 | .remove = __exit_p(ramoops_remove), | |
202 | .driver = { | |
203 | .name = "ramoops", | |
204 | .owner = THIS_MODULE, | |
205 | }, | |
206 | }; | |
207 | ||
208 | static int __init ramoops_init(void) | |
209 | { | |
13aefd72 MS |
210 | int ret; |
211 | ret = platform_driver_probe(&ramoops_driver, ramoops_probe); | |
212 | if (ret == -ENODEV) { | |
213 | /* | |
214 | * If we didn't find a platform device, we use module parameters | |
215 | * building platform data on the fly. | |
216 | */ | |
0169256e | 217 | pr_info("platform device not found, using module parameters\n"); |
13aefd72 MS |
218 | dummy_data = kzalloc(sizeof(struct ramoops_platform_data), |
219 | GFP_KERNEL); | |
220 | if (!dummy_data) | |
221 | return -ENOMEM; | |
222 | dummy_data->mem_size = mem_size; | |
223 | dummy_data->mem_address = mem_address; | |
3e5c4fad | 224 | dummy_data->record_size = record_size; |
6b4d2a27 | 225 | dummy_data->dump_oops = dump_oops; |
13aefd72 MS |
226 | dummy = platform_create_bundle(&ramoops_driver, ramoops_probe, |
227 | NULL, 0, dummy_data, | |
228 | sizeof(struct ramoops_platform_data)); | |
229 | ||
230 | if (IS_ERR(dummy)) | |
231 | ret = PTR_ERR(dummy); | |
232 | else | |
233 | ret = 0; | |
234 | } | |
235 | ||
236 | return ret; | |
c3b92ce9 KP |
237 | } |
238 | ||
239 | static void __exit ramoops_exit(void) | |
240 | { | |
241 | platform_driver_unregister(&ramoops_driver); | |
13aefd72 | 242 | kfree(dummy_data); |
c3b92ce9 | 243 | } |
56d611a0 MS |
244 | |
245 | module_init(ramoops_init); | |
246 | module_exit(ramoops_exit); | |
247 | ||
248 | MODULE_LICENSE("GPL"); | |
249 | MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>"); | |
250 | MODULE_DESCRIPTION("RAM Oops/Panic logger/driver"); |