apanic.c 14 KB

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