blob: fde08712247364e3f6921974caf802b6c94d3a93 [file] [log] [blame]
San Mehat01c99682009-09-01 13:43:01 -07001/* drivers/misc/apanic.c
2 *
3 * Copyright (C) 2009 Google, Inc.
4 * Author: San Mehat <san@android.com>
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16
17#include <linux/module.h>
18#include <linux/kernel.h>
19#include <linux/string.h>
20#include <linux/errno.h>
21#include <linux/init.h>
22#include <linux/interrupt.h>
23#include <linux/device.h>
24#include <linux/types.h>
25#include <linux/delay.h>
26#include <linux/sched.h>
27#include <linux/wait.h>
28#include <linux/wakelock.h>
29#include <linux/platform_device.h>
30#include <linux/uaccess.h>
31#include <linux/mtd/mtd.h>
32#include <linux/notifier.h>
33#include <linux/mtd/mtd.h>
34#include <linux/debugfs.h>
35#include <linux/fs.h>
36#include <linux/proc_fs.h>
37#include <linux/mutex.h>
38#include <linux/workqueue.h>
39#include <linux/preempt.h>
40
41struct panic_header {
42 u32 magic;
43#define PANIC_MAGIC 0xdeadf00d
44
45 u32 version;
46#define PHDR_VERSION 0x01
47
48 u32 console_offset;
49 u32 console_length;
50
51 u32 threads_offset;
52 u32 threads_length;
53};
54
55#define CHECK_BB 0
56
57struct apanic_data {
58 struct mtd_info *mtd;
59 struct panic_header curr;
60 void *bounce;
61 struct proc_dir_entry *apanic_console;
62 struct proc_dir_entry *apanic_threads;
63};
64
65static struct apanic_data drv_ctx;
66static struct work_struct proc_removal_work;
67static DEFINE_MUTEX(drv_mutex);
68
69static void apanic_erase_callback(struct erase_info *done)
70{
71 wait_queue_head_t *wait_q = (wait_queue_head_t *) done->priv;
72 wake_up(wait_q);
73}
74
75static int apanic_proc_read(char *buffer, char **start, off_t offset,
76 int count, int *peof, void *dat)
77{
78 struct apanic_data *ctx = &drv_ctx;
79 size_t file_length;
80 off_t file_offset;
81 unsigned int page_no;
82 off_t page_offset;
83 int rc;
84 size_t len;
85
86 if (!count)
87 return 0;
88
89 mutex_lock(&drv_mutex);
90
91 switch ((int) dat) {
92 case 1: /* apanic_console */
93 file_length = ctx->curr.console_length;
94 file_offset = ctx->curr.console_offset;
95 break;
96 case 2: /* apanic_threads */
97 file_length = ctx->curr.threads_length;
98 file_offset = ctx->curr.threads_offset;
99 break;
100 default:
101 pr_err("Bad dat (%d)\n", (int) dat);
102 mutex_unlock(&drv_mutex);
103 return -EINVAL;
104 }
105
106 if ((offset + count) > file_length) {
107 mutex_unlock(&drv_mutex);
108 return 0;
109 }
110
111 /* We only support reading a maximum of a flash page */
112 if (count > ctx->mtd->writesize)
113 count = ctx->mtd->writesize;
114
115 page_no = (file_offset + offset) / ctx->mtd->writesize;
116 page_offset = (file_offset + offset) % ctx->mtd->writesize;
117
118 rc = ctx->mtd->read(ctx->mtd,
119 (page_no * ctx->mtd->writesize),
120 ctx->mtd->writesize,
121 &len, ctx->bounce);
122
123 if (page_offset)
124 count -= page_offset;
125 memcpy(buffer, ctx->bounce + page_offset, count);
126
127 *start = count;
128
129 if ((offset + count) == file_length)
130 *peof = 1;
131
132 mutex_unlock(&drv_mutex);
133 return count;
134}
135
136static void mtd_panic_erase(void)
137{
138 struct apanic_data *ctx = &drv_ctx;
139 struct erase_info erase;
140 DECLARE_WAITQUEUE(wait, current);
141 wait_queue_head_t wait_q;
142 int rc, i;
143
144 init_waitqueue_head(&wait_q);
145 erase.mtd = ctx->mtd;
146 erase.callback = apanic_erase_callback;
147 erase.len = ctx->mtd->erasesize;
148 erase.priv = (u_long)&wait_q;
149 for (i = 0; i < ctx->mtd->size; i += ctx->mtd->erasesize) {
150 erase.addr = i;
151 set_current_state(TASK_INTERRUPTIBLE);
152 add_wait_queue(&wait_q, &wait);
153
154 rc = ctx->mtd->block_isbad(ctx->mtd, erase.addr);
155 if (rc < 0) {
156 printk(KERN_ERR
157 "apanic: Bad block check "
158 "failed (%d)\n", rc);
159 goto out;
160 }
161 if (rc) {
162 printk(KERN_WARNING
163 "apanic: Skipping erase of bad "
164 "block @%llx\n", erase.addr);
165 set_current_state(TASK_RUNNING);
166 remove_wait_queue(&wait_q, &wait);
167 continue;
168 }
169
170 rc = ctx->mtd->erase(ctx->mtd, &erase);
171 if (rc) {
172 set_current_state(TASK_RUNNING);
173 remove_wait_queue(&wait_q, &wait);
174 printk(KERN_ERR
175 "apanic: Erase of 0x%llx, 0x%llx failed\n",
176 (unsigned long long) erase.addr,
177 (unsigned long long) erase.len);
178 if (rc == -EIO) {
179 if (ctx->mtd->block_markbad(ctx->mtd,
180 erase.addr)) {
181 printk(KERN_ERR
182 "apanic: Err marking blk bad\n");
183 goto out;
184 }
185 printk(KERN_INFO
186 "apanic: Marked a bad block"
187 " @%llx\n", erase.addr);
188 continue;
189 }
190 goto out;
191 }
192 schedule();
193 remove_wait_queue(&wait_q, &wait);
194 }
195 printk(KERN_DEBUG "apanic: %s partition erased\n",
196 CONFIG_APANIC_PLABEL);
197out:
198 return;
199}
200
201static void apanic_remove_proc_work(struct work_struct *work)
202{
203 struct apanic_data *ctx = &drv_ctx;
204
205 mutex_lock(&drv_mutex);
206 mtd_panic_erase();
207 memset(&ctx->curr, 0, sizeof(struct panic_header));
208 if (ctx->apanic_console) {
209 remove_proc_entry("apanic_console", NULL);
210 ctx->apanic_console = NULL;
211 }
212 if (ctx->apanic_threads) {
213 remove_proc_entry("apanic_threads", NULL);
214 ctx->apanic_threads = NULL;
215 }
216 mutex_unlock(&drv_mutex);
217}
218
219static int apanic_proc_write(struct file *file, const char __user *buffer,
220 unsigned long count, void *data)
221{
222 schedule_work(&proc_removal_work);
223 return count;
224}
225
226static void mtd_panic_notify_add(struct mtd_info *mtd)
227{
228 struct apanic_data *ctx = &drv_ctx;
229 struct panic_header *hdr = ctx->bounce;
230 size_t len;
231 int rc;
232
233 if (strcmp(mtd->name, CONFIG_APANIC_PLABEL))
234 return;
235
236 ctx->mtd = mtd;
237
238 if (mtd->block_isbad(mtd, 0)) {
239 printk(KERN_ERR "apanic: Offset 0 bad block. Boourns!\n");
240 goto out_err;
241 }
242
243 rc = mtd->read(mtd, 0, mtd->writesize, &len, ctx->bounce);
244 if (rc && rc == -EBADMSG) {
245 printk(KERN_WARNING
246 "apanic: Bad ECC on block 0 (ignored)\n");
247 } else if (rc && rc != -EUCLEAN) {
248 printk(KERN_ERR "apanic: Error reading block 0 (%d)\n", rc);
249 goto out_err;
250 }
251
252 if (len != mtd->writesize) {
253 printk(KERN_ERR "apanic: Bad read size (%d)\n", rc);
254 goto out_err;
255 }
256
257 printk(KERN_INFO "apanic: Bound to mtd partition '%s'\n", mtd->name);
258
259 if (hdr->magic != PANIC_MAGIC) {
260 printk(KERN_INFO "apanic: No panic data available\n");
261 mtd_panic_erase();
262 return;
263 }
264
265 if (hdr->version != PHDR_VERSION) {
266 printk(KERN_INFO "apanic: Version mismatch (%d != %d)\n",
267 hdr->version, PHDR_VERSION);
268 mtd_panic_erase();
269 return;
270 }
271
272 memcpy(&ctx->curr, hdr, sizeof(struct panic_header));
273
274 printk(KERN_INFO "apanic: c(%u, %u) t(%u, %u)\n",
275 hdr->console_offset, hdr->console_length,
276 hdr->threads_offset, hdr->threads_length);
277
278 if (hdr->console_length) {
279 ctx->apanic_console = create_proc_entry("apanic_console",
280 S_IFREG | S_IRUGO, NULL);
281 if (!ctx->apanic_console)
282 printk(KERN_ERR "%s: failed creating procfile\n",
283 __func__);
284 else {
285 ctx->apanic_console->read_proc = apanic_proc_read;
286 ctx->apanic_console->write_proc = apanic_proc_write;
287 ctx->apanic_console->size = hdr->console_length;
288 ctx->apanic_console->data = (void *) 1;
289 }
290 }
291
292 if (hdr->threads_length) {
293 ctx->apanic_threads = create_proc_entry("apanic_threads",
294 S_IFREG | S_IRUGO, NULL);
295 if (!ctx->apanic_threads)
296 printk(KERN_ERR "%s: failed creating procfile\n",
297 __func__);
298 else {
299 ctx->apanic_threads->read_proc = apanic_proc_read;
300 ctx->apanic_threads->write_proc = apanic_proc_write;
301 ctx->apanic_threads->size = hdr->threads_length;
302 ctx->apanic_threads->data = (void *) 2;
303 }
304 }
305
306 return;
307out_err:
308 ctx->mtd = NULL;
309}
310
311static void mtd_panic_notify_remove(struct mtd_info *mtd)
312{
313 struct apanic_data *ctx = &drv_ctx;
314 if (mtd == ctx->mtd) {
315 ctx->mtd = NULL;
316 printk(KERN_INFO "apanic: Unbound from %s\n", mtd->name);
317 }
318}
319
320static struct mtd_notifier mtd_panic_notifier = {
321 .add = mtd_panic_notify_add,
322 .remove = mtd_panic_notify_remove,
323};
324
325static int in_panic = 0;
326
327static int apanic_writeflashpage(struct mtd_info *mtd, loff_t to,
328 const u_char *buf)
329{
330 int rc;
331 size_t wlen;
332 int panic = in_interrupt() | in_atomic();
333
334 if (panic && !mtd->panic_write) {
335 printk(KERN_EMERG "%s: No panic_write available\n", __func__);
336 return 0;
337 } else if (!panic && !mtd->write) {
338 printk(KERN_EMERG "%s: No write available\n", __func__);
339 return 0;
340 }
341
342 if (panic)
343 rc = mtd->panic_write(mtd, to, mtd->writesize, &wlen, buf);
344 else
345 rc = mtd->write(mtd, to, mtd->writesize, &wlen, buf);
346
347 if (rc) {
348 printk(KERN_EMERG
349 "%s: Error writing data to flash (%d)\n",
350 __func__, rc);
351 return rc;
352 }
353
354 return wlen;
355}
356
357extern int log_buf_copy(char *dest, int idx, int len);
358extern void log_buf_clear(void);
359
360/*
361 * Writes the contents of the console to the specified offset in flash.
362 * Returns number of bytes written
363 */
364static int apanic_write_console(struct mtd_info *mtd, unsigned int off)
365{
366 struct apanic_data *ctx = &drv_ctx;
367 int saved_oip;
368 int idx = 0;
369 int rc, rc2;
370 unsigned int last_chunk = 0;
371
372 while (!last_chunk) {
373 saved_oip = oops_in_progress;
374 oops_in_progress = 1;
375 rc = log_buf_copy(ctx->bounce, idx, mtd->writesize);
376 if (rc < 0)
377 break;
378
379 if (rc != mtd->writesize)
380 last_chunk = rc;
381
382 oops_in_progress = saved_oip;
383 if (rc <= 0)
384 break;
385 if (rc != mtd->writesize)
386 memset(ctx->bounce + rc, 0, mtd->writesize - rc);
387#if CHECK_BB
388check_badblock:
389 rc = mtd->block_isbad(mtd, off);
390 if (rc < 0) {
391 printk(KERN_ERR
392 "apanic: Bad block check "
393 "failed (%d)\n", rc);
394 }
395 if (rc) {
396 printk(KERN_WARNING
397 "apanic: Skipping over bad "
398 "block @%x\n", off);
399 off += mtd->erasesize;
400 printk("chk %u %llu\n", off, mtd->size);
401 if (off >= mtd->size) {
402 printk(KERN_EMERG
403 "apanic: Too many bad blocks!\n");
404 return -EIO;
405 }
406 goto check_badblock;
407 }
408#endif
409
410 rc2 = apanic_writeflashpage(mtd, off, ctx->bounce);
411 if (rc2 <= 0) {
412 printk(KERN_EMERG
413 "apanic: Flash write failed (%d)\n", rc2);
414 return rc2;
415 }
416 if (!last_chunk)
417 idx += rc2;
418 else
419 idx += last_chunk;
420 off += rc2;
421 }
422 return idx;
423}
424
425static int apanic(struct notifier_block *this, unsigned long event,
426 void *ptr)
427{
428 struct apanic_data *ctx = &drv_ctx;
429 struct panic_header *hdr = (struct panic_header *) ctx->bounce;
430 int console_offset = 0;
431 int console_len = 0;
432 int threads_offset = 0;
433 int threads_len = 0;
434 int rc;
435
436 if (in_panic)
437 return NOTIFY_DONE;
438 in_panic = 1;
439#ifdef CONFIG_PREEMPT
440 /* Ensure that cond_resched() won't try to preempt anybody */
441 add_preempt_count(PREEMPT_ACTIVE);
442#endif
443
444 if (!ctx->mtd)
445 goto out;
446
447 if (ctx->curr.magic) {
448 printk(KERN_EMERG "Crash partition in use!\n");
449 goto out;
450 }
451 console_offset = ctx->mtd->writesize;
452
453 /*
454 * Write out the console
455 */
456 console_len = apanic_write_console(ctx->mtd, console_offset);
457 if (console_len < 0) {
458 printk(KERN_EMERG "Error writing console to panic log! (%d)\n",
459 console_len);
460 console_len = 0;
461 }
462
463 /*
464 * Write out all threads
465 */
466 threads_offset = ALIGN(console_offset + console_len,
467 ctx->mtd->writesize);
468 if (!threads_offset)
469 threads_offset = ctx->mtd->writesize;
470
471 log_buf_clear();
472 show_state_filter(0);
473 threads_len = apanic_write_console(ctx->mtd, threads_offset);
474 if (threads_len < 0) {
475 printk(KERN_EMERG "Error writing threads to panic log! (%d)\n",
476 threads_len);
477 threads_len = 0;
478 }
479
480 /*
481 * Finally write the panic header
482 */
483 memset(ctx->bounce, 0, PAGE_SIZE);
484 hdr->magic = PANIC_MAGIC;
485 hdr->version = PHDR_VERSION;
486
487 hdr->console_offset = console_offset;
488 hdr->console_length = console_len;
489
490 hdr->threads_offset = threads_offset;
491 hdr->threads_length = threads_len;
492
493 rc = apanic_writeflashpage(ctx->mtd, 0, ctx->bounce);
494 if (rc <= 0) {
495 printk(KERN_EMERG "apanic: Header write failed (%d)\n",
496 rc);
497 goto out;
498 }
499
500 printk(KERN_EMERG "apanic: Panic dump sucessfully written to flash\n");
501
502 out:
503#ifdef CONFIG_PREEMPT
504 sub_preempt_count(PREEMPT_ACTIVE);
505#endif
506 in_panic = 0;
507 return NOTIFY_DONE;
508}
509
510static struct notifier_block panic_blk = {
511 .notifier_call = apanic,
512};
513
514static int panic_dbg_get(void *data, u64 *val)
515{
516 apanic(NULL, 0, NULL);
517 return 0;
518}
519
520static int panic_dbg_set(void *data, u64 val)
521{
522 BUG();
523 return -1;
524}
525
526DEFINE_SIMPLE_ATTRIBUTE(panic_dbg_fops, panic_dbg_get, panic_dbg_set, "%llu\n");
527
528int __init apanic_init(void)
529{
530 register_mtd_user(&mtd_panic_notifier);
531 atomic_notifier_chain_register(&panic_notifier_list, &panic_blk);
532 debugfs_create_file("apanic", 0644, NULL, NULL, &panic_dbg_fops);
533 memset(&drv_ctx, 0, sizeof(drv_ctx));
534 drv_ctx.bounce = (void *) __get_free_page(GFP_KERNEL);
535 INIT_WORK(&proc_removal_work, apanic_remove_proc_work);
536 printk(KERN_INFO "Android kernel panic handler initialized (bind=%s)\n",
537 CONFIG_APANIC_PLABEL);
538 return 0;
539}
540
541module_init(apanic_init);