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