ram.c 13 KB

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