1 /* drivers/misc/apanic.c
3 * Copyright (C) 2009 Google, Inc.
4 * Author: San Mehat <san@android.com>
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.
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.
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>
36 #include <linux/proc_fs.h>
37 #include <linux/mutex.h>
38 #include <linux/workqueue.h>
39 #include <linux/preempt.h>
41 extern void ram_console_enable_console(int);
45 #define PANIC_MAGIC 0xdeadf00d
48 #define PHDR_VERSION 0x01
59 struct panic_header curr
;
61 struct proc_dir_entry
*apanic_console
;
62 struct proc_dir_entry
*apanic_threads
;
65 static struct apanic_data drv_ctx
;
66 static struct work_struct proc_removal_work
;
67 static DEFINE_MUTEX(drv_mutex
);
69 static unsigned int *apanic_bbt
;
70 static unsigned int apanic_erase_blocks
;
71 static unsigned int apanic_good_blocks
;
73 static void set_bb(unsigned int block
, unsigned int *bbt
)
75 unsigned int flag
= 1;
77 BUG_ON(block
>= apanic_erase_blocks
);
79 flag
= flag
<< (block
%32);
80 apanic_bbt
[block
/32] |= flag
;
84 static unsigned int get_bb(unsigned int block
, unsigned int *bbt
)
88 BUG_ON(block
>= apanic_erase_blocks
);
90 flag
= 1 << (block
%32);
91 return apanic_bbt
[block
/32] & flag
;
94 static void alloc_bbt(struct mtd_info
*mtd
, unsigned int *bbt
)
97 apanic_erase_blocks
= (mtd
->size
)>>(mtd
->erasesize_shift
);
98 bbt_size
= (apanic_erase_blocks
+32)/32;
100 apanic_bbt
= kmalloc(bbt_size
*4, GFP_KERNEL
);
101 memset(apanic_bbt
, 0, bbt_size
*4);
102 apanic_good_blocks
= apanic_erase_blocks
;
104 static void scan_bbt(struct mtd_info
*mtd
, unsigned int *bbt
)
108 for (i
= 0; i
< apanic_erase_blocks
; i
++) {
109 if (mtd
->block_isbad(mtd
, i
*mtd
->erasesize
))
110 set_bb(i
, apanic_bbt
);
114 #define APANIC_INVALID_OFFSET 0xFFFFFFFF
116 static unsigned int phy_offset(struct mtd_info
*mtd
, unsigned int offset
)
118 unsigned int logic_block
= offset
>>(mtd
->erasesize_shift
);
119 unsigned int phy_block
;
120 unsigned good_block
= 0;
122 for (phy_block
= 0; phy_block
< apanic_erase_blocks
; phy_block
++) {
123 if (!get_bb(phy_block
, apanic_bbt
))
125 if (good_block
== (logic_block
+ 1))
129 if (good_block
!= (logic_block
+ 1))
130 return APANIC_INVALID_OFFSET
;
132 return offset
+ ((phy_block
-logic_block
)<<mtd
->erasesize_shift
);
135 static void apanic_erase_callback(struct erase_info
*done
)
137 wait_queue_head_t
*wait_q
= (wait_queue_head_t
*) done
->priv
;
141 static int apanic_proc_read(char *buffer
, char **start
, off_t offset
,
142 int count
, int *peof
, void *dat
)
144 struct apanic_data
*ctx
= &drv_ctx
;
147 unsigned int page_no
;
155 mutex_lock(&drv_mutex
);
158 case 1: /* apanic_console */
159 file_length
= ctx
->curr
.console_length
;
160 file_offset
= ctx
->curr
.console_offset
;
162 case 2: /* apanic_threads */
163 file_length
= ctx
->curr
.threads_length
;
164 file_offset
= ctx
->curr
.threads_offset
;
167 pr_err("Bad dat (%d)\n", (int) dat
);
168 mutex_unlock(&drv_mutex
);
172 if ((offset
+ count
) > file_length
) {
173 mutex_unlock(&drv_mutex
);
177 /* We only support reading a maximum of a flash page */
178 if (count
> ctx
->mtd
->writesize
)
179 count
= ctx
->mtd
->writesize
;
181 page_no
= (file_offset
+ offset
) / ctx
->mtd
->writesize
;
182 page_offset
= (file_offset
+ offset
) % ctx
->mtd
->writesize
;
185 if (phy_offset(ctx
->mtd
, (page_no
* ctx
->mtd
->writesize
))
186 == APANIC_INVALID_OFFSET
) {
187 pr_err("apanic: reading an invalid address\n");
188 mutex_unlock(&drv_mutex
);
191 rc
= ctx
->mtd
->read(ctx
->mtd
,
192 phy_offset(ctx
->mtd
, (page_no
* ctx
->mtd
->writesize
)),
197 count
-= page_offset
;
198 memcpy(buffer
, ctx
->bounce
+ page_offset
, count
);
202 if ((offset
+ count
) == file_length
)
205 mutex_unlock(&drv_mutex
);
209 static void mtd_panic_erase(void)
211 struct apanic_data
*ctx
= &drv_ctx
;
212 struct erase_info erase
;
213 DECLARE_WAITQUEUE(wait
, current
);
214 wait_queue_head_t wait_q
;
217 init_waitqueue_head(&wait_q
);
218 erase
.mtd
= ctx
->mtd
;
219 erase
.callback
= apanic_erase_callback
;
220 erase
.len
= ctx
->mtd
->erasesize
;
221 erase
.priv
= (u_long
)&wait_q
;
222 for (i
= 0; i
< ctx
->mtd
->size
; i
+= ctx
->mtd
->erasesize
) {
224 set_current_state(TASK_INTERRUPTIBLE
);
225 add_wait_queue(&wait_q
, &wait
);
227 if (get_bb(erase
.addr
>>ctx
->mtd
->erasesize_shift
, apanic_bbt
)) {
229 "apanic: Skipping erase of bad "
230 "block @%llx\n", erase
.addr
);
231 set_current_state(TASK_RUNNING
);
232 remove_wait_queue(&wait_q
, &wait
);
236 rc
= ctx
->mtd
->erase(ctx
->mtd
, &erase
);
238 set_current_state(TASK_RUNNING
);
239 remove_wait_queue(&wait_q
, &wait
);
241 "apanic: Erase of 0x%llx, 0x%llx failed\n",
242 (unsigned long long) erase
.addr
,
243 (unsigned long long) erase
.len
);
245 if (ctx
->mtd
->block_markbad(ctx
->mtd
,
248 "apanic: Err marking blk bad\n");
252 "apanic: Marked a bad block"
253 " @%llx\n", erase
.addr
);
254 set_bb(erase
.addr
>>ctx
->mtd
->erasesize_shift
,
261 remove_wait_queue(&wait_q
, &wait
);
263 printk(KERN_DEBUG
"apanic: %s partition erased\n",
264 CONFIG_APANIC_PLABEL
);
269 static void apanic_remove_proc_work(struct work_struct
*work
)
271 struct apanic_data
*ctx
= &drv_ctx
;
273 mutex_lock(&drv_mutex
);
275 memset(&ctx
->curr
, 0, sizeof(struct panic_header
));
276 if (ctx
->apanic_console
) {
277 remove_proc_entry("apanic_console", NULL
);
278 ctx
->apanic_console
= NULL
;
280 if (ctx
->apanic_threads
) {
281 remove_proc_entry("apanic_threads", NULL
);
282 ctx
->apanic_threads
= NULL
;
284 mutex_unlock(&drv_mutex
);
287 static int apanic_proc_write(struct file
*file
, const char __user
*buffer
,
288 unsigned long count
, void *data
)
290 schedule_work(&proc_removal_work
);
294 static void mtd_panic_notify_add(struct mtd_info
*mtd
)
296 struct apanic_data
*ctx
= &drv_ctx
;
297 struct panic_header
*hdr
= ctx
->bounce
;
300 int proc_entry_created
= 0;
302 if (strcmp(mtd
->name
, CONFIG_APANIC_PLABEL
))
307 alloc_bbt(mtd
, apanic_bbt
);
308 scan_bbt(mtd
, apanic_bbt
);
310 if (apanic_good_blocks
== 0) {
311 printk(KERN_ERR
"apanic: no any good blocks?!\n");
315 rc
= mtd
->read(mtd
, phy_offset(mtd
, 0), mtd
->writesize
,
317 if (rc
&& rc
== -EBADMSG
) {
319 "apanic: Bad ECC on block 0 (ignored)\n");
320 } else if (rc
&& rc
!= -EUCLEAN
) {
321 printk(KERN_ERR
"apanic: Error reading block 0 (%d)\n", rc
);
325 if (len
!= mtd
->writesize
) {
326 printk(KERN_ERR
"apanic: Bad read size (%d)\n", rc
);
330 printk(KERN_INFO
"apanic: Bound to mtd partition '%s'\n", mtd
->name
);
332 if (hdr
->magic
!= PANIC_MAGIC
) {
333 printk(KERN_INFO
"apanic: No panic data available\n");
338 if (hdr
->version
!= PHDR_VERSION
) {
339 printk(KERN_INFO
"apanic: Version mismatch (%d != %d)\n",
340 hdr
->version
, PHDR_VERSION
);
345 memcpy(&ctx
->curr
, hdr
, sizeof(struct panic_header
));
347 printk(KERN_INFO
"apanic: c(%u, %u) t(%u, %u)\n",
348 hdr
->console_offset
, hdr
->console_length
,
349 hdr
->threads_offset
, hdr
->threads_length
);
351 if (hdr
->console_length
) {
352 ctx
->apanic_console
= create_proc_entry("apanic_console",
353 S_IFREG
| S_IRUGO
, NULL
);
354 if (!ctx
->apanic_console
)
355 printk(KERN_ERR
"%s: failed creating procfile\n",
358 ctx
->apanic_console
->read_proc
= apanic_proc_read
;
359 ctx
->apanic_console
->write_proc
= apanic_proc_write
;
360 ctx
->apanic_console
->size
= hdr
->console_length
;
361 ctx
->apanic_console
->data
= (void *) 1;
362 proc_entry_created
= 1;
366 if (hdr
->threads_length
) {
367 ctx
->apanic_threads
= create_proc_entry("apanic_threads",
368 S_IFREG
| S_IRUGO
, NULL
);
369 if (!ctx
->apanic_threads
)
370 printk(KERN_ERR
"%s: failed creating procfile\n",
373 ctx
->apanic_threads
->read_proc
= apanic_proc_read
;
374 ctx
->apanic_threads
->write_proc
= apanic_proc_write
;
375 ctx
->apanic_threads
->size
= hdr
->threads_length
;
376 ctx
->apanic_threads
->data
= (void *) 2;
377 proc_entry_created
= 1;
381 if (!proc_entry_created
)
389 static void mtd_panic_notify_remove(struct mtd_info
*mtd
)
391 struct apanic_data
*ctx
= &drv_ctx
;
392 if (mtd
== ctx
->mtd
) {
394 printk(KERN_INFO
"apanic: Unbound from %s\n", mtd
->name
);
398 static struct mtd_notifier mtd_panic_notifier
= {
399 .add
= mtd_panic_notify_add
,
400 .remove
= mtd_panic_notify_remove
,
403 static int in_panic
= 0;
405 static int apanic_writeflashpage(struct mtd_info
*mtd
, loff_t to
,
410 int panic
= in_interrupt() | in_atomic();
412 if (panic
&& !mtd
->panic_write
) {
413 printk(KERN_EMERG
"%s: No panic_write available\n", __func__
);
415 } else if (!panic
&& !mtd
->write
) {
416 printk(KERN_EMERG
"%s: No write available\n", __func__
);
420 to
= phy_offset(mtd
, to
);
421 if (to
== APANIC_INVALID_OFFSET
) {
422 printk(KERN_EMERG
"apanic: write to invalid address\n");
427 rc
= mtd
->panic_write(mtd
, to
, mtd
->writesize
, &wlen
, buf
);
429 rc
= mtd
->write(mtd
, to
, mtd
->writesize
, &wlen
, buf
);
433 "%s: Error writing data to flash (%d)\n",
441 extern int log_buf_copy(char *dest
, int idx
, int len
);
442 extern void log_buf_clear(void);
445 * Writes the contents of the console to the specified offset in flash.
446 * Returns number of bytes written
448 static int apanic_write_console(struct mtd_info
*mtd
, unsigned int off
)
450 struct apanic_data
*ctx
= &drv_ctx
;
454 unsigned int last_chunk
= 0;
456 while (!last_chunk
) {
457 saved_oip
= oops_in_progress
;
458 oops_in_progress
= 1;
459 rc
= log_buf_copy(ctx
->bounce
, idx
, mtd
->writesize
);
463 if (rc
!= mtd
->writesize
)
466 oops_in_progress
= saved_oip
;
469 if (rc
!= mtd
->writesize
)
470 memset(ctx
->bounce
+ rc
, 0, mtd
->writesize
- rc
);
472 rc2
= apanic_writeflashpage(mtd
, off
, ctx
->bounce
);
475 "apanic: Flash write failed (%d)\n", rc2
);
487 static int apanic(struct notifier_block
*this, unsigned long event
,
490 struct apanic_data
*ctx
= &drv_ctx
;
491 struct panic_header
*hdr
= (struct panic_header
*) ctx
->bounce
;
492 int console_offset
= 0;
494 int threads_offset
= 0;
501 #ifdef CONFIG_PREEMPT
502 /* Ensure that cond_resched() won't try to preempt anybody */
503 add_preempt_count(PREEMPT_ACTIVE
);
505 touch_softlockup_watchdog();
510 if (ctx
->curr
.magic
) {
511 printk(KERN_EMERG
"Crash partition in use!\n");
514 console_offset
= ctx
->mtd
->writesize
;
517 * Write out the console
519 console_len
= apanic_write_console(ctx
->mtd
, console_offset
);
520 if (console_len
< 0) {
521 printk(KERN_EMERG
"Error writing console to panic log! (%d)\n",
527 * Write out all threads
529 threads_offset
= ALIGN(console_offset
+ console_len
,
530 ctx
->mtd
->writesize
);
532 threads_offset
= ctx
->mtd
->writesize
;
534 ram_console_enable_console(0);
537 show_state_filter(0);
538 threads_len
= apanic_write_console(ctx
->mtd
, threads_offset
);
539 if (threads_len
< 0) {
540 printk(KERN_EMERG
"Error writing threads to panic log! (%d)\n",
546 * Finally write the panic header
548 memset(ctx
->bounce
, 0, PAGE_SIZE
);
549 hdr
->magic
= PANIC_MAGIC
;
550 hdr
->version
= PHDR_VERSION
;
552 hdr
->console_offset
= console_offset
;
553 hdr
->console_length
= console_len
;
555 hdr
->threads_offset
= threads_offset
;
556 hdr
->threads_length
= threads_len
;
558 rc
= apanic_writeflashpage(ctx
->mtd
, 0, ctx
->bounce
);
560 printk(KERN_EMERG
"apanic: Header write failed (%d)\n",
565 printk(KERN_EMERG
"apanic: Panic dump sucessfully written to flash\n");
568 #ifdef CONFIG_PREEMPT
569 sub_preempt_count(PREEMPT_ACTIVE
);
575 static struct notifier_block panic_blk
= {
576 .notifier_call
= apanic
,
579 static int panic_dbg_get(void *data
, u64
*val
)
581 apanic(NULL
, 0, NULL
);
585 static int panic_dbg_set(void *data
, u64 val
)
591 DEFINE_SIMPLE_ATTRIBUTE(panic_dbg_fops
, panic_dbg_get
, panic_dbg_set
, "%llu\n");
593 int __init
apanic_init(void)
595 register_mtd_user(&mtd_panic_notifier
);
596 atomic_notifier_chain_register(&panic_notifier_list
, &panic_blk
);
597 debugfs_create_file("apanic", 0644, NULL
, NULL
, &panic_dbg_fops
);
598 memset(&drv_ctx
, 0, sizeof(drv_ctx
));
599 drv_ctx
.bounce
= (void *) __get_free_page(GFP_KERNEL
);
600 INIT_WORK(&proc_removal_work
, apanic_remove_proc_work
);
601 printk(KERN_INFO
"Android kernel panic handler initialized (bind=%s)\n",
602 CONFIG_APANIC_PLABEL
);
606 module_init(apanic_init
);