ram.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. * RAM Oops/Panic logger
  3. *
  4. * Copyright (C) 2010 Marco Stornelli <marco.stornelli@gmail.com>
  5. * Copyright (C) 2011 Kees Cook <keescook@chromium.org>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  19. * 02110-1301 USA
  20. *
  21. */
  22. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include <linux/kernel.h>
  24. #include <linux/err.h>
  25. #include <linux/module.h>
  26. #include <linux/pstore.h>
  27. #include <linux/time.h>
  28. #include <linux/io.h>
  29. #include <linux/ioport.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/slab.h>
  32. #include <linux/pstore_ram.h>
  33. #define RAMOOPS_KERNMSG_HDR "===="
  34. #define MIN_MEM_SIZE 4096UL
  35. static ulong record_size = MIN_MEM_SIZE;
  36. module_param(record_size, ulong, 0400);
  37. MODULE_PARM_DESC(record_size,
  38. "size of each dump done on oops/panic");
  39. static ulong ramoops_console_size = MIN_MEM_SIZE;
  40. module_param_named(console_size, ramoops_console_size, ulong, 0400);
  41. MODULE_PARM_DESC(console_size, "size of kernel console log");
  42. static ulong ramoops_ftrace_size = MIN_MEM_SIZE;
  43. module_param_named(ftrace_size, ramoops_ftrace_size, ulong, 0400);
  44. MODULE_PARM_DESC(ftrace_size, "size of ftrace log");
  45. static ulong mem_address;
  46. module_param(mem_address, ulong, 0400);
  47. MODULE_PARM_DESC(mem_address,
  48. "start of reserved RAM used to store oops/panic logs");
  49. static ulong mem_size;
  50. module_param(mem_size, ulong, 0400);
  51. MODULE_PARM_DESC(mem_size,
  52. "size of reserved RAM used to store oops/panic logs");
  53. static int dump_oops = 1;
  54. module_param(dump_oops, int, 0600);
  55. MODULE_PARM_DESC(dump_oops,
  56. "set to 1 to dump oopses, 0 to only dump panics (default 1)");
  57. static int ramoops_ecc;
  58. module_param_named(ecc, ramoops_ecc, int, 0600);
  59. MODULE_PARM_DESC(ramoops_ecc,
  60. "if non-zero, the option enables ECC support and specifies "
  61. "ECC buffer size in bytes (1 is a special value, means 16 "
  62. "bytes ECC)");
  63. struct ramoops_context {
  64. struct persistent_ram_zone **przs;
  65. struct persistent_ram_zone *cprz;
  66. struct persistent_ram_zone *fprz;
  67. phys_addr_t phys_addr;
  68. unsigned long size;
  69. size_t record_size;
  70. size_t console_size;
  71. size_t ftrace_size;
  72. int dump_oops;
  73. int ecc_size;
  74. unsigned int max_dump_cnt;
  75. unsigned int dump_write_cnt;
  76. unsigned int dump_read_cnt;
  77. unsigned int console_read_cnt;
  78. unsigned int ftrace_read_cnt;
  79. struct pstore_info pstore;
  80. };
  81. static struct platform_device *dummy;
  82. static struct ramoops_platform_data *dummy_data;
  83. static int ramoops_pstore_open(struct pstore_info *psi)
  84. {
  85. struct ramoops_context *cxt = psi->data;
  86. cxt->dump_read_cnt = 0;
  87. cxt->console_read_cnt = 0;
  88. return 0;
  89. }
  90. static struct persistent_ram_zone *
  91. ramoops_get_next_prz(struct persistent_ram_zone *przs[], uint *c, uint max,
  92. u64 *id,
  93. enum pstore_type_id *typep, enum pstore_type_id type,
  94. bool update)
  95. {
  96. struct persistent_ram_zone *prz;
  97. int i = (*c)++;
  98. if (i >= max)
  99. return NULL;
  100. prz = przs[i];
  101. if (update) {
  102. /* Update old/shadowed buffer. */
  103. persistent_ram_save_old(prz);
  104. if (!persistent_ram_old_size(prz))
  105. return NULL;
  106. }
  107. *typep = type;
  108. *id = i;
  109. return prz;
  110. }
  111. static ssize_t ramoops_pstore_read(u64 *id, enum pstore_type_id *type,
  112. struct timespec *time,
  113. char **buf,
  114. struct pstore_info *psi)
  115. {
  116. ssize_t size;
  117. struct ramoops_context *cxt = psi->data;
  118. struct persistent_ram_zone *prz;
  119. prz = ramoops_get_next_prz(cxt->przs, &cxt->dump_read_cnt,
  120. cxt->max_dump_cnt, id, type,
  121. PSTORE_TYPE_DMESG, 1);
  122. if (!prz)
  123. prz = ramoops_get_next_prz(&cxt->cprz, &cxt->console_read_cnt,
  124. 1, id, type, PSTORE_TYPE_CONSOLE, 0);
  125. if (!prz)
  126. prz = ramoops_get_next_prz(&cxt->fprz, &cxt->ftrace_read_cnt,
  127. 1, id, type, PSTORE_TYPE_FTRACE, 0);
  128. if (!prz)
  129. return 0;
  130. /* TODO(kees): Bogus time for the moment. */
  131. time->tv_sec = 0;
  132. time->tv_nsec = 0;
  133. size = persistent_ram_old_size(prz);
  134. *buf = kmalloc(size, GFP_KERNEL);
  135. if (*buf == NULL)
  136. return -ENOMEM;
  137. memcpy(*buf, persistent_ram_old(prz), size);
  138. return size;
  139. }
  140. static size_t ramoops_write_kmsg_hdr(struct persistent_ram_zone *prz)
  141. {
  142. char *hdr;
  143. struct timeval timestamp;
  144. size_t len;
  145. do_gettimeofday(&timestamp);
  146. hdr = kasprintf(GFP_ATOMIC, RAMOOPS_KERNMSG_HDR "%lu.%lu\n",
  147. (long)timestamp.tv_sec, (long)timestamp.tv_usec);
  148. WARN_ON_ONCE(!hdr);
  149. len = hdr ? strlen(hdr) : 0;
  150. persistent_ram_write(prz, hdr, len);
  151. kfree(hdr);
  152. return len;
  153. }
  154. static int ramoops_pstore_write_buf(enum pstore_type_id type,
  155. enum kmsg_dump_reason reason,
  156. u64 *id, unsigned int part,
  157. const char *buf, size_t size,
  158. struct pstore_info *psi)
  159. {
  160. struct ramoops_context *cxt = psi->data;
  161. struct persistent_ram_zone *prz = cxt->przs[cxt->dump_write_cnt];
  162. size_t hlen;
  163. if (type == PSTORE_TYPE_CONSOLE) {
  164. if (!cxt->cprz)
  165. return -ENOMEM;
  166. persistent_ram_write(cxt->cprz, buf, size);
  167. return 0;
  168. } else if (type == PSTORE_TYPE_FTRACE) {
  169. if (!cxt->fprz)
  170. return -ENOMEM;
  171. persistent_ram_write(cxt->fprz, buf, size);
  172. return 0;
  173. }
  174. if (type != PSTORE_TYPE_DMESG)
  175. return -EINVAL;
  176. /* Out of the various dmesg dump types, ramoops is currently designed
  177. * to only store crash logs, rather than storing general kernel logs.
  178. */
  179. if (reason != KMSG_DUMP_OOPS &&
  180. reason != KMSG_DUMP_PANIC)
  181. return -EINVAL;
  182. /* Skip Oopes when configured to do so. */
  183. if (reason == KMSG_DUMP_OOPS && !cxt->dump_oops)
  184. return -EINVAL;
  185. /* Explicitly only take the first part of any new crash.
  186. * If our buffer is larger than kmsg_bytes, this can never happen,
  187. * and if our buffer is smaller than kmsg_bytes, we don't want the
  188. * report split across multiple records.
  189. */
  190. if (part != 1)
  191. return -ENOSPC;
  192. hlen = ramoops_write_kmsg_hdr(prz);
  193. if (size + hlen > prz->buffer_size)
  194. size = prz->buffer_size - hlen;
  195. persistent_ram_write(prz, buf, size);
  196. cxt->dump_write_cnt = (cxt->dump_write_cnt + 1) % cxt->max_dump_cnt;
  197. return 0;
  198. }
  199. static int ramoops_pstore_erase(enum pstore_type_id type, u64 id,
  200. struct pstore_info *psi)
  201. {
  202. struct ramoops_context *cxt = psi->data;
  203. struct persistent_ram_zone *prz;
  204. switch (type) {
  205. case PSTORE_TYPE_DMESG:
  206. if (id >= cxt->max_dump_cnt)
  207. return -EINVAL;
  208. prz = cxt->przs[id];
  209. break;
  210. case PSTORE_TYPE_CONSOLE:
  211. prz = cxt->cprz;
  212. break;
  213. case PSTORE_TYPE_FTRACE:
  214. prz = cxt->fprz;
  215. break;
  216. default:
  217. return -EINVAL;
  218. }
  219. persistent_ram_free_old(prz);
  220. persistent_ram_zap(prz);
  221. return 0;
  222. }
  223. static struct ramoops_context oops_cxt = {
  224. .pstore = {
  225. .owner = THIS_MODULE,
  226. .name = "ramoops",
  227. .open = ramoops_pstore_open,
  228. .read = ramoops_pstore_read,
  229. .write_buf = ramoops_pstore_write_buf,
  230. .erase = ramoops_pstore_erase,
  231. },
  232. };
  233. static void ramoops_free_przs(struct ramoops_context *cxt)
  234. {
  235. int i;
  236. if (!cxt->przs)
  237. return;
  238. for (i = 0; !IS_ERR_OR_NULL(cxt->przs[i]); i++)
  239. persistent_ram_free(cxt->przs[i]);
  240. kfree(cxt->przs);
  241. }
  242. static int ramoops_init_przs(struct device *dev, struct ramoops_context *cxt,
  243. phys_addr_t *paddr, size_t dump_mem_sz)
  244. {
  245. int err = -ENOMEM;
  246. int i;
  247. if (!cxt->record_size)
  248. return 0;
  249. cxt->max_dump_cnt = dump_mem_sz / cxt->record_size;
  250. if (!cxt->max_dump_cnt)
  251. return -ENOMEM;
  252. cxt->przs = kzalloc(sizeof(*cxt->przs) * cxt->max_dump_cnt,
  253. GFP_KERNEL);
  254. if (!cxt->przs) {
  255. dev_err(dev, "failed to initialize a prz array for dumps\n");
  256. return -ENOMEM;
  257. }
  258. for (i = 0; i < cxt->max_dump_cnt; i++) {
  259. size_t sz = cxt->record_size;
  260. cxt->przs[i] = persistent_ram_new(*paddr, sz, cxt->ecc_size);
  261. if (IS_ERR(cxt->przs[i])) {
  262. err = PTR_ERR(cxt->przs[i]);
  263. dev_err(dev, "failed to request mem region (0x%zx@0x%llx): %d\n",
  264. sz, (unsigned long long)*paddr, err);
  265. goto fail_prz;
  266. }
  267. *paddr += sz;
  268. }
  269. return 0;
  270. fail_prz:
  271. ramoops_free_przs(cxt);
  272. return err;
  273. }
  274. static int ramoops_init_prz(struct device *dev, struct ramoops_context *cxt,
  275. struct persistent_ram_zone **prz,
  276. phys_addr_t *paddr, size_t sz)
  277. {
  278. if (!sz)
  279. return 0;
  280. if (*paddr + sz > *paddr + cxt->size)
  281. return -ENOMEM;
  282. *prz = persistent_ram_new(*paddr, sz, cxt->ecc_size);
  283. if (IS_ERR(*prz)) {
  284. int err = PTR_ERR(*prz);
  285. dev_err(dev, "failed to request mem region (0x%zx@0x%llx): %d\n",
  286. sz, (unsigned long long)*paddr, err);
  287. return err;
  288. }
  289. persistent_ram_zap(*prz);
  290. *paddr += sz;
  291. return 0;
  292. }
  293. static int __devinit ramoops_probe(struct platform_device *pdev)
  294. {
  295. struct device *dev = &pdev->dev;
  296. struct ramoops_platform_data *pdata = pdev->dev.platform_data;
  297. struct ramoops_context *cxt = &oops_cxt;
  298. size_t dump_mem_sz;
  299. phys_addr_t paddr;
  300. int err = -EINVAL;
  301. /* Only a single ramoops area allowed at a time, so fail extra
  302. * probes.
  303. */
  304. if (cxt->max_dump_cnt)
  305. goto fail_out;
  306. if (!pdata->mem_size || (!pdata->record_size && !pdata->console_size &&
  307. !pdata->ftrace_size)) {
  308. pr_err("The memory size and the record/console size must be "
  309. "non-zero\n");
  310. goto fail_out;
  311. }
  312. pdata->mem_size = rounddown_pow_of_two(pdata->mem_size);
  313. pdata->record_size = rounddown_pow_of_two(pdata->record_size);
  314. pdata->console_size = rounddown_pow_of_two(pdata->console_size);
  315. pdata->ftrace_size = rounddown_pow_of_two(pdata->ftrace_size);
  316. cxt->dump_read_cnt = 0;
  317. cxt->size = pdata->mem_size;
  318. cxt->phys_addr = pdata->mem_address;
  319. cxt->record_size = pdata->record_size;
  320. cxt->console_size = pdata->console_size;
  321. cxt->ftrace_size = pdata->ftrace_size;
  322. cxt->dump_oops = pdata->dump_oops;
  323. cxt->ecc_size = pdata->ecc_size;
  324. paddr = cxt->phys_addr;
  325. dump_mem_sz = cxt->size - cxt->console_size - cxt->ftrace_size;
  326. err = ramoops_init_przs(dev, cxt, &paddr, dump_mem_sz);
  327. if (err)
  328. goto fail_out;
  329. err = ramoops_init_prz(dev, cxt, &cxt->cprz, &paddr, cxt->console_size);
  330. if (err)
  331. goto fail_init_cprz;
  332. err = ramoops_init_prz(dev, cxt, &cxt->fprz, &paddr, cxt->ftrace_size);
  333. if (err)
  334. goto fail_init_fprz;
  335. if (!cxt->przs && !cxt->cprz && !cxt->fprz) {
  336. pr_err("memory size too small, minimum is %lu\n",
  337. cxt->console_size + cxt->record_size +
  338. cxt->ftrace_size);
  339. goto fail_cnt;
  340. }
  341. cxt->pstore.data = cxt;
  342. /*
  343. * Console can handle any buffer size, so prefer dumps buffer
  344. * size since usually it is smaller.
  345. */
  346. if (cxt->przs)
  347. cxt->pstore.bufsize = cxt->przs[0]->buffer_size;
  348. else
  349. cxt->pstore.bufsize = cxt->cprz->buffer_size;
  350. cxt->pstore.buf = kmalloc(cxt->pstore.bufsize, GFP_KERNEL);
  351. spin_lock_init(&cxt->pstore.buf_lock);
  352. if (!cxt->pstore.buf) {
  353. pr_err("cannot allocate pstore buffer\n");
  354. goto fail_clear;
  355. }
  356. err = pstore_register(&cxt->pstore);
  357. if (err) {
  358. pr_err("registering with pstore failed\n");
  359. goto fail_buf;
  360. }
  361. /*
  362. * Update the module parameter variables as well so they are visible
  363. * through /sys/module/ramoops/parameters/
  364. */
  365. mem_size = pdata->mem_size;
  366. mem_address = pdata->mem_address;
  367. record_size = pdata->record_size;
  368. dump_oops = pdata->dump_oops;
  369. pr_info("attached 0x%lx@0x%llx, ecc: %d\n",
  370. cxt->size, (unsigned long long)cxt->phys_addr,
  371. cxt->ecc_size);
  372. return 0;
  373. fail_buf:
  374. kfree(cxt->pstore.buf);
  375. fail_clear:
  376. cxt->pstore.bufsize = 0;
  377. cxt->max_dump_cnt = 0;
  378. fail_cnt:
  379. kfree(cxt->fprz);
  380. fail_init_fprz:
  381. kfree(cxt->cprz);
  382. fail_init_cprz:
  383. ramoops_free_przs(cxt);
  384. fail_out:
  385. return err;
  386. }
  387. static int __exit ramoops_remove(struct platform_device *pdev)
  388. {
  389. #if 0
  390. /* TODO(kees): We cannot unload ramoops since pstore doesn't support
  391. * unregistering yet.
  392. */
  393. struct ramoops_context *cxt = &oops_cxt;
  394. iounmap(cxt->virt_addr);
  395. release_mem_region(cxt->phys_addr, cxt->size);
  396. cxt->max_dump_cnt = 0;
  397. /* TODO(kees): When pstore supports unregistering, call it here. */
  398. kfree(cxt->pstore.buf);
  399. cxt->pstore.bufsize = 0;
  400. return 0;
  401. #endif
  402. return -EBUSY;
  403. }
  404. static struct platform_driver ramoops_driver = {
  405. .probe = ramoops_probe,
  406. .remove = __exit_p(ramoops_remove),
  407. .driver = {
  408. .name = "ramoops",
  409. .owner = THIS_MODULE,
  410. },
  411. };
  412. static void ramoops_register_dummy(void)
  413. {
  414. if (!mem_size)
  415. return;
  416. pr_info("using module parameters\n");
  417. dummy_data = kzalloc(sizeof(*dummy_data), GFP_KERNEL);
  418. if (!dummy_data) {
  419. pr_info("could not allocate pdata\n");
  420. return;
  421. }
  422. dummy_data->mem_size = mem_size;
  423. dummy_data->mem_address = mem_address;
  424. dummy_data->record_size = record_size;
  425. dummy_data->console_size = ramoops_console_size;
  426. dummy_data->ftrace_size = ramoops_ftrace_size;
  427. dummy_data->dump_oops = dump_oops;
  428. /*
  429. * For backwards compatibility ramoops.ecc=1 means 16 bytes ECC
  430. * (using 1 byte for ECC isn't much of use anyway).
  431. */
  432. dummy_data->ecc_size = ramoops_ecc == 1 ? 16 : ramoops_ecc;
  433. dummy = platform_device_register_data(NULL, "ramoops", -1,
  434. dummy_data, sizeof(struct ramoops_platform_data));
  435. if (IS_ERR(dummy)) {
  436. pr_info("could not create platform device: %ld\n",
  437. PTR_ERR(dummy));
  438. }
  439. }
  440. static int __init ramoops_init(void)
  441. {
  442. ramoops_register_dummy();
  443. return platform_driver_register(&ramoops_driver);
  444. }
  445. postcore_initcall(ramoops_init);
  446. static void __exit ramoops_exit(void)
  447. {
  448. platform_driver_unregister(&ramoops_driver);
  449. kfree(dummy_data);
  450. }
  451. module_exit(ramoops_exit);
  452. MODULE_LICENSE("GPL");
  453. MODULE_AUTHOR("Marco Stornelli <marco.stornelli@gmail.com>");
  454. MODULE_DESCRIPTION("RAM Oops/Panic logger/driver");