ram.c 14 KB

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