1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
24#include <linux/kernel.h>
25#include <linux/err.h>
26#include <linux/module.h>
27#include <linux/kmsg_dump.h>
28#include <linux/time.h>
29#include <linux/err.h>
30#include <linux/io.h>
31#include <linux/ioport.h>
32#include <linux/platform_device.h>
33#include <linux/slab.h>
34#include <linux/ramoops.h>
35
36#define RAMOOPS_KERNMSG_HDR "===="
37#define MIN_MEM_SIZE 4096UL
38
39static ulong record_size = MIN_MEM_SIZE;
40module_param(record_size, ulong, 0400);
41MODULE_PARM_DESC(record_size,
42 "size of each dump done on oops/panic");
43
44static ulong mem_address;
45module_param(mem_address, ulong, 0400);
46MODULE_PARM_DESC(mem_address,
47 "start of reserved RAM used to store oops/panic logs");
48
49static ulong mem_size;
50module_param(mem_size, ulong, 0400);
51MODULE_PARM_DESC(mem_size,
52 "size of reserved RAM used to store oops/panic logs");
53
54static int dump_oops = 1;
55module_param(dump_oops, int, 0600);
56MODULE_PARM_DESC(dump_oops,
57 "set to 1 to dump oopses, 0 to only dump panics (default 1)");
58
59static struct ramoops_context {
60 struct kmsg_dumper dump;
61 void *virt_addr;
62 phys_addr_t phys_addr;
63 unsigned long size;
64 unsigned long record_size;
65 int dump_oops;
66 int count;
67 int max_count;
68} oops_cxt;
69
70static struct platform_device *dummy;
71static struct ramoops_platform_data *dummy_data;
72
73static void ramoops_do_dump(struct kmsg_dumper *dumper,
74 enum kmsg_dump_reason reason, const char *s1, unsigned long l1,
75 const char *s2, unsigned long l2)
76{
77 struct ramoops_context *cxt = container_of(dumper,
78 struct ramoops_context, dump);
79 unsigned long s1_start, s2_start;
80 unsigned long l1_cpy, l2_cpy;
81 int res, hdr_size;
82 char *buf, *buf_orig;
83 struct timeval timestamp;
84
85 if (reason != KMSG_DUMP_OOPS &&
86 reason != KMSG_DUMP_PANIC &&
87 reason != KMSG_DUMP_KEXEC)
88 return;
89
90
91 if (reason == KMSG_DUMP_OOPS && !cxt->dump_oops)
92 return;
93
94 buf = cxt->virt_addr + (cxt->count * cxt->record_size);
95 buf_orig = buf;
96
97 memset(buf, '\0', cxt->record_size);
98 res = sprintf(buf, "%s", RAMOOPS_KERNMSG_HDR);
99 buf += res;
100 do_gettimeofday(×tamp);
101 res = sprintf(buf, "%lu.%lu\n", (long)timestamp.tv_sec, (long)timestamp.tv_usec);
102 buf += res;
103
104 hdr_size = buf - buf_orig;
105 l2_cpy = min(l2, cxt->record_size - hdr_size);
106 l1_cpy = min(l1, cxt->record_size - hdr_size - l2_cpy);
107
108 s2_start = l2 - l2_cpy;
109 s1_start = l1 - l1_cpy;
110
111 memcpy(buf, s1 + s1_start, l1_cpy);
112 memcpy(buf + l1_cpy, s2 + s2_start, l2_cpy);
113
114 cxt->count = (cxt->count + 1) % cxt->max_count;
115}
116
117static int __init ramoops_probe(struct platform_device *pdev)
118{
119 struct ramoops_platform_data *pdata = pdev->dev.platform_data;
120 struct ramoops_context *cxt = &oops_cxt;
121 int err = -EINVAL;
122
123 if (!pdata->mem_size || !pdata->record_size) {
124 pr_err("The memory size and the record size must be "
125 "non-zero\n");
126 goto fail3;
127 }
128
129 rounddown_pow_of_two(pdata->mem_size);
130 rounddown_pow_of_two(pdata->record_size);
131
132
133 if (pdata->mem_size < MIN_MEM_SIZE &&
134 pdata->record_size < MIN_MEM_SIZE) {
135 pr_err("memory size too small, minium is %lu\n", MIN_MEM_SIZE);
136 goto fail3;
137 }
138
139 if (pdata->mem_size < pdata->record_size) {
140 pr_err("The memory size must be larger than the "
141 "records size\n");
142 goto fail3;
143 }
144
145 cxt->max_count = pdata->mem_size / pdata->record_size;
146 cxt->count = 0;
147 cxt->size = pdata->mem_size;
148 cxt->phys_addr = pdata->mem_address;
149 cxt->record_size = pdata->record_size;
150 cxt->dump_oops = pdata->dump_oops;
151
152
153
154
155 mem_size = pdata->mem_size;
156 mem_address = pdata->mem_address;
157 record_size = pdata->record_size;
158 dump_oops = pdata->dump_oops;
159
160 if (!request_mem_region(cxt->phys_addr, cxt->size, "ramoops")) {
161 pr_err("request mem region failed\n");
162 err = -EINVAL;
163 goto fail3;
164 }
165
166 cxt->virt_addr = ioremap(cxt->phys_addr, cxt->size);
167 if (!cxt->virt_addr) {
168 pr_err("ioremap failed\n");
169 goto fail2;
170 }
171
172 cxt->dump.dump = ramoops_do_dump;
173 err = kmsg_dump_register(&cxt->dump);
174 if (err) {
175 pr_err("registering kmsg dumper failed\n");
176 goto fail1;
177 }
178
179 return 0;
180
181fail1:
182 iounmap(cxt->virt_addr);
183fail2:
184 release_mem_region(cxt->phys_addr, cxt->size);
185fail3:
186 return err;
187}
188
189static int __exit ramoops_remove(struct platform_device *pdev)
190{
191 struct ramoops_context *cxt = &oops_cxt;
192
193 if (kmsg_dump_unregister(&cxt->dump) < 0)
194 pr_warn("could not unregister kmsg_dumper\n");
195
196 iounmap(cxt->virt_addr);
197 release_mem_region(cxt->phys_addr, cxt->size);
198 return 0;
199}
200
201static struct platform_driver ramoops_driver = {
202 .remove = __exit_p(ramoops_remove),
203 .driver = {
204 .name = "ramoops",
205 .owner = THIS_MODULE,
206 },
207};
208
209static int __init ramoops_init(void)
210{
211 int ret;
212 ret = platform_driver_probe(&ramoops_driver, ramoops_probe);
213 if (ret == -ENODEV) {
214
215
216
217
218 pr_info("platform device not found, using module parameters\n");
219 dummy_data = kzalloc(sizeof(struct ramoops_platform_data),
220 GFP_KERNEL);
221 if (!dummy_data)
222 return -ENOMEM;
223 dummy_data->mem_size = mem_size;
224 dummy_data->mem_address = mem_address;
225 dummy_data->record_size = record_size;
226 dummy_data->dump_oops = dump_oops;
227 dummy = platform_create_bundle(&ramoops_driver, ramoops_probe,
228 NULL, 0, dummy_data,
229 sizeof(struct ramoops_platform_data));
230
231 if (IS_ERR(dummy))
232 ret = PTR_ERR(dummy);
233 else
234 ret = 0;
235 }
236
237 return ret;
238}
239
240static void __exit ramoops_exit(void)
241{
242 platform_driver_unregister(&ramoops_driver);
243 kfree(dummy_data);
244}
245
246module_init(ramoops_init);
247module_exit(ramoops_exit);
248
249MODULE_LICENSE("GPL");
250MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
251MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");
252